【发布时间】:2016-03-24 17:07:03
【问题描述】:
我正在开发一个 PHP 网络服务,它将对应用程序 API 进行 REST 调用。我的网络服务建立在运行 ubuntu 的 LAMP 服务器上,以下命令在命令行中完美运行:
curl -k -u 用户名 -H "Content-type: application/xml" -X PUT -d @request.xml https://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 的变量。无论如何,任何帮助将不胜感激。
【问题讨论】:
-
man curl并查看-XPUT和-d -
嗨 Sammitch,我正在寻找的是 PHP 等价物。不过我会检查一下。