simplexml_load_file not working

Mark picture Mark · Jun 30, 2011 · Viewed 32.9k times · Source

I have this piece of code below which works fine on my remote hosted server, but isnt for some reason working on my local linux machine. Ive tried using file_get_contents as well to get the restful service but it also returns false.

Does anyone know Why this is happening?

thanks :)

$xml_data = simplexml_load_file("****");

if ($xml == FALSE)
{
  echo "Failed loading XML\n";

  foreach (libxml_get_errors() as $error) 
  {
    echo "\t", $error->message;
  }   
} 

Answer

Jigar Tank picture Jigar Tank · Nov 11, 2011

You are getting this error because remote file access has been disabled on your server. An alternative to this is using CURL.

Use my code below to use CURL:

function produce_XML_object_tree($raw_XML) {
    libxml_use_internal_errors(true);
    try {
        $xmlTree = new SimpleXMLElement($raw_XML);
    } catch (Exception $e) {
        // Something went wrong.
        $error_message = 'SimpleXMLElement threw an exception.';
        foreach(libxml_get_errors() as $error_line) {
            $error_message .= "\t" . $error_line->message;
        }
        trigger_error($error_message);
        return false;
    }
    return $xmlTree;
}

$xml_feed_url = '******';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);

$cont = produce_XML_object_tree($xml);

Now use $cont as an object to access different nodes in the xml.