Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplexml_load_file not working

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;
  }   
} 
like image 800
Mark Avatar asked Jun 30 '11 02:06

Mark


1 Answers

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.

like image 109
Jigar Tank Avatar answered Oct 19 '22 23:10

Jigar Tank