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 に答える