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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.