Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中复制命令行cURL_Php_Curl - Fatal编程技术网

在PHP中复制命令行cURL

在PHP中复制命令行cURL,php,curl,Php,Curl,我正在与第三方的API集成,我必须发布一些XML,然后得到一些XML 在CLI上,我得到了积极的响应 curl -X POST -d @/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded" 但是,这不起作用,响应包含一个错误,告诉我我的请求XML无效 $ch = curl_init(); $post = array( 'file' => '@/tmp/fi

我正在与第三方的API集成,我必须发布一些XML,然后得到一些XML

在CLI上,我得到了积极的响应

curl -X POST -d @/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded"
但是,这不起作用,响应包含一个错误,告诉我我的请求XML无效

$ch = curl_init();

$post = array(
  'file' => '@/tmp/file'
);

curl_setopt($ch, CURLOPT_URL,             'http://url/to/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,  true);
curl_setopt($ch, CURLOPT_POST,            true);
curl_setopt($ch, CURLOPT_HTTPHEADER,      array('Content-type:application/x-www-form-urlencoded'));

curl_setopt($ch, CURLOPT_POSTFIELDS,      $post);

$this->responseBody = curl_exec($ch);

curl_close($ch);
在这两种情况下都是同一个文件,并且在同一台服务器上。该文件只是纯文本XML。我能看到的唯一区别是,我在PHP版本的HTTP头中指定了一个字段名

如何使用PHP发送该文件以准确复制CLI版本,例如不使用formdata/fieldname位


FWIW我有几天不能回到API开发人员那里去问他定义了什么“坏XML”

尝试将文件作为原始数据传递,而不是以数组形式传递,例如使用
file\u get\u contents()

因此,不是:

$post = array('file' => '@/tmp/file');
像这样:

$post = file_get_contents('@/tmp/file');

您是否尝试过curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:text/xml'));也不确定,但您可能必须在内容类型中大写type尝试将文件作为原始数据传递,而不是以数组传递,例如file_get_contents。必须设置内容类型:application/x-www-form-urlencoded&Content encoding:text/plain,如下所示,否则我会得到一个与内容类型相关的错误。@silkfire这就是它的全部内容,谢谢++你想将它添加为一个答案,我会关闭它吗?