PHP CURL PUT等效于命令行


PHP CURL PUT Equivalent to Command Line

我正在开发一个PHP Web服务,该服务将对应用程序API进行REST调用。我的Web服务建立在运行ubuntu的LAMP服务器上,以下命令在命令行中运行良好:

curl-k-u username-H"内容类型:application/xml"-X PUT-d@request.xmlhttps://server/path/

然而,当我尝试用PHP构建相同的请求时,我从REST服务器得到了以下响应,表明XML丢失了:

HTTP400文件过早结束。

以下是我的PHP请求代码:

$ch2 = curl_init($service_url);
$headers = array('Content-Type: application/xml', 'Accept: application/xml');
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_PUT, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch2, CURLOPT_USERPWD, 'user:password');
$curl_response = curl_exec($ch2);

我所读到的一切都表明CURLOPT_POSTFIELDS是这样做的地方,但我认为这是为了在实际URL中添加"?"变量。不管怎样,任何帮助都将不胜感激。

根据手册,如果您使用CURLOPT_PUT,您还应该提供CURLOPT_INFILECURLOPT_INFILESIZE

也许不是

curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);

尝试

curl_setopt($ch2, CURLOPT_INFILE, fopen('request.xml', 'r'));
curl_setopt($ch2, CURLOPT_INFILESIZE, filesize('request.xml'));