[英]replicating command line cURL in PHP
I'm integrating with a 3rd party's API, I have to POST some XML and I get some XML back. 我正在与第三方的API集成,我必须发布一些XML,然后获得一些XML。
On the CLI this works, I get a positive response. 在CLI上这有效,我得到了肯定的答复。
curl -X POST -d @/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded"
This, however, does not work, the response contains an error telling me my that my request XML is invalid. 但是,这不起作用,响应包含一个错误,告诉我我的请求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);
It's the same file in both cases and it's on the same server. 两种情况下都是相同的文件,并且在同一台服务器上。 the file is just plain text XML.
该文件只是纯文本XML。 The only difference that I can see is that I'm specifying a fieldname in my HTTP headers on the PHP version.
我可以看到的唯一区别是,我在PHP版本的HTTP标头中指定了一个字段名。
How do I send that file over using PHP to exactly replicate the CLI version, eg without the formdata/fieldname bit? 如何使用PHP发送该文件以完全复制CLI版本,例如没有formdata / fieldname位?
FWIW I can't go back to the developer of the API for a few days to ask what he's defining as 'bad XML' FWIW,我几天不能再回头问API的开发者了,他将其定义为“不良XML”
Try passing the file as raw data, not in an array, by for example using file_get_contents()
. 尝试通过例如使用
file_get_contents()
将文件作为原始数据而不是数组传递。
So instead of: 所以代替:
$post = array('file' => '@/tmp/file');
Like this: 像这样:
$post = file_get_contents('@/tmp/file');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.