simplexml_load_file not working
Asked Answered
G

7

8

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;
  }   
} 
Gershwin answered 30/6, 2011 at 2:56 Comment(2)
Can you do something like file_get_contents("http://www.example.com"); to verify that you can call any remote file?Diplocardiac
10 years! and no one noticed that if ($xml_data == FALSE) and not as above... :-)Lossa
C
11

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.

Calaverite answered 11/11, 2011 at 11:59 Comment(0)
D
7

Make sure you have allow_url_fopen turned on in your php.ini

http://php.net/manual/filesystem.configuration.php

Diplocardiac answered 30/6, 2011 at 2:58 Comment(1)
@Gershwin Weird. I wouldn't think they would depend directly on one another.Diplocardiac
M
3

Well I had same issue and though I would post this to assist anyone who may have not tried this solution yet.

I had a PHP script which worked fine locally, but when using it on a client server running plesk it would not work and failed when trying to grab the external xml file.

I was trying to reference an external xml file from a php script. The server I was using was running plesk. Before considering changing host, All I simply did was update the settings for PHP on the server to run as an Apache Module instead of FastCGI.

error message which I was receiving (example):

Warning: simplexml_load_file(url) [function.simplexml-load-file]: failed to open stream: Permission denied

This resolved the issue in my case.

I used following reports settings in the PHP script:

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_BAIL, 1);
assert_options(ASSERT_QUIET_EVAL, 1);
error_reporting(E_ALL);
ini_set('display_errors', 1);
Microseism answered 5/10, 2012 at 17:19 Comment(0)
S
2

Change: if ($xml == FALSE) to if ($xml === FALSE) (source).

Saphead answered 1/5, 2012 at 14:45 Comment(0)
I
1

use like this

$xml = simplexml_load_file('http://localhost/test/123.xml');

foreach ($xml->children() as $child) {
    $remoteCount[$child->getName()] = $child;

}
var_dump($remoteCount);
Incise answered 31/1, 2012 at 19:10 Comment(0)
L
0

I had the same problem it's just a stupid undeclared point in the simplexml

the xml file format should have a container tag, so, you just have to put a parent tag containing all your data like this:

<?xml version="1.0">
<data>
    ...all your file content here...
</data>
Lisa answered 28/5, 2014 at 7:1 Comment(0)
F
0

In my case, it's missing the XML php library, reinstall it and works fine

https://wpml.org/forums/topic/fatal-error-uncaught-error-call-to-undefined-function-simplexml_load_file-3/

Former answered 10/1, 2023 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.