PHP: send a POST request and then read the XML response?

I am trying to write a PHP script that sends a POST request to a remote server and then parses the XML response.

I can execute the POST request, but I'm having difficulties (from other SO questions) that you are good at with 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 - line simplexml_load_file($response) .

I work on a local server, not sure if that matters. 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?

+4
source share
3 answers

use simplexml_load_string instead of simplexml_load_file

+8
source

You must set the cURL option to return the transfer

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
+3
source

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

 // Instead of $rxml = simplexml_load_file($response); // You want $rxml = simplexml_load_string($response); 
+1
source

Source: https://habr.com/ru/post/956428/


All Articles