Jump to content

cURL and SimpleXML


upgrader

Recommended Posts

I'm trying to use data returned from a cURL request, the page returned is xml. When I try and load the returned data with simplexml_load_file I get the error saying simplexml does not recognize the file.

 

The error:

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

 

This is the xml response from curl:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><response><results><![CDATA[An error occured %s]]></results><status><![CDATA[]]></status></response>

 

Here is a snippet of my code:

 

curl_setopt($ch, CURLOPT_URL, 
'myurl');

$result = curl_exec($ch);

curl_close($ch);

$xml = simplexml_load_file($result);

(not the whole code, don't worry the cURL part works fine)

 

Is there something special I have to do when trying to use data from a cURL request?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/
Share on other sites

<?php
$xml = new SimpleXMLElement( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<response>
    <results>
        <![CDATA[An error occured %s]]>
    </results>
    <status>
        <![CDATA[]]>
    </status>
</response>" );
print_r( $xml );
?>

That works for me.

 

Try adding a var_dump( $result ) to your code and see what $result actually is.

Link to comment
https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/#findComment-887911
Share on other sites

Still get the same error:

 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8" standalone="yes"?><response><results><![CDATA[An error occured: %s]]></results><status><![CDATA[]]></status></response>"

 

With this code:

 

curl_setopt($ch, CURLOPT_URL, 
'myurl');

$result = curl_exec($ch);

curl_close($ch);

$result = (string)$result;

$xml = simplexml_load_file($result);

 

Edit: got it working, used simplexml_load_string instead.

Link to comment
https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/#findComment-888176
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.