PHP:发送POST请求然后读取XML响应?

时间:2011-05-18 12:51:44

标签: php xml post simplexml

我正在尝试编写一个PHP脚本,将POST请求发送到远程服务器,然后解析XML响应。

我可以执行POST请求,但是很难(从其他SO问题)解决如何解析XML响应。

我当前的代码为我提供了Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "1" in /Users/simon/usercreate.php on line 46 - simplexml_load_file($response)行。

我正在使用本地服务器,不确定这是否有所作为。代码:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$response = curl_exec ($curl);
curl_close ($curl);
echo $response;
$rxml = simplexml_load_file($response);
echo $rxml->title;

我做错了什么?

3 个答案:

答案 0 :(得分:8)

使用simplexml_load_string代替simplexml_load_file

答案 1 :(得分:3)

您必须设置cURL选项才能返回转移

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

答案 2 :(得分:1)

您希望加载字符串,而不是加载文件。

// Instead of
$rxml = simplexml_load_file($response);

// You want
$rxml = simplexml_load_string($response);
相关问题