我的目标是向服务器发送POST请求并获得正确的响应.
注意:有角括号代表占位符.
在终端中,使用以下代码将为我提供所需的响应.
curl -u : -H 'Content-Type: application/xml' -X POST https://
我当前的PHP看起来像这样:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri); //$uri is the same that I use in terminal
curl_setopt($ch, CURLOPT_USERPWD,
sprintf('%s:%s', $user, $pass)); //same as terminal user & pass
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array(
'Content-Type: application/xml', //expect an xml response
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_result = curl_exec($ch);
使用这个PHP,我收到400 Bad Request错误.
详细信息:
> POST HTTP/1.1
Authorization: Basic YWRtaW5Ac3Bhcms0NTEuY29tOnNwYXJrc29tZXRoaW5n
Host:
Accept: */*
Content-Type: application/xml
Content-Length: -1
Expect: 100-continue
* HTTP 1.0, assume close after body
< HTTP/1.0 400 Bad request
< Cache-Control: no-cache
< Connection: close
< Content-Type: text/html
为什么我在使用PHP时遇到400 Bad Request错误,但在使用命令行时却没有?如何解决此问题,以便使用PHP获得所需的响应?