simplexml_load_file not working

2019-04-28 19:39发布

问题:

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;
  }   
} 

回答1:

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.



回答2:

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

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



回答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);


回答4:

use like this

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

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

}
var_dump($remoteCount);


回答5:

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



回答6:

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>