1

我创建了一个使用 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
4

1 回答 1

0

我找到了解决方案,必须在 curl 请求中添加用户代理。

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
于 2015-07-13T16:21:21.240 回答