简体   繁体   English

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

[英]PHP: send POST request then read XML response?

I'm trying to write a PHP script that sends a POST request to a remote server, then parses the XML response. 我正在尝试编写一个PHP脚本,将POST请求发送到远程服务器,然后解析XML响应。

I can do the POST request, but am having difficulty (from other SO questions) working out how to parse the XML response. 我可以做POST请求,但是很难(从其他SO问题)解决如何解析XML响应。

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. 我当前的代码告诉我: 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)一行。

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? 我究竟做错了什么?

使用simplexml_load_string而不是simplexml_load_file

您必须设置cURL选项才能返回传输

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM