upgrader Posted July 31, 2009 Share Posted July 31, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/ Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/#findComment-887911 Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 Checking the documentation, curl_exec() returns a resource, not a string. Try casting it to a (string) when you use it. Quote Link to comment https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/#findComment-887912 Share on other sites More sharing options...
upgrader Posted August 1, 2009 Author Share Posted August 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168321-curl-and-simplexml/#findComment-888176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.