我创建了一个使用 POST 发送 xml 请求的脚本,它可以在 linux 命令行上完美运行,但不能在 php curl 中运行,这是我的 xml 文件和我使用的命令。
<?xml version="1.0"?>
<methodCall>
<methodName>test.echo</methodName>
<params>
<param><value><string>nbb</string></value></param>
<param><value><string>health check: 1436777963</string></value></param>
</params></methodCall>
curl -H "Content-Type: text/xml" -d @test.xml -X POST http:my.host:5862 -v
这是我的 php 脚本,
$url = 'http://my.host:5862';
#$requested = xml_gen($function_name,$epos,$time);
$requested = '<?xml version="1.0"?><methodCall><methodName>test.echo</methodName><params><param><value><string>nbb</string></value></param><param><value><string>health check: 1436777963</string></value></param></params></methodCall>';
#echo($requested);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING ,"ISO-8859-1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $requested);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
echo $response = curl_exec($ch);
curl_close($ch);
//Decoding the response to be displayed
echo xmlrpc_decode($response);
这是我使用 PHP 的 cURL 时遇到的错误。
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 13 Jul 2015 15:26:05 GMT
Content-Type: text/XML
Content-Length: 0
Connection: keep-alive
Keep-Alive: timeout=20