PHP: send POST request then read XML response?

╄→гoц情女王★ 提交于 2019-12-07 08:14:00

问题


I'm trying to write a PHP script that sends a POST request to a remote server, then parses the XML response.

I can do the POST request, but am having difficulty (from other SO questions) working out how to parse the XML response.

My current code gives me: 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 - the simplexml_load_file($response) line.

I'm working on a local server, not sure if that makes a difference. Code:

$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;

What am I doing wrong?


回答1:


use simplexml_load_string instead of simplexml_load_file




回答2:


You have to set the cURL option to return the transfer

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);



回答3:


Instead of loading a file, you want to load a string.

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

// You want
$rxml = simplexml_load_string($response);


来源:https://stackoverflow.com/questions/6044941/php-send-post-request-then-read-xml-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!