Gavin Mannion Gavin Mannion - 1 year ago 400
PHP Question

SimpleXML - I/O warning : failed to load external entity

I'm trying to create a small application that will simply read an RSS feed and then layout the info on the page.

All the instructions I find make this seem simplistic but for some reason it just isn't working. I have the following

$feed = file_get_contents('');
$items = simplexml_load_file($feed);

That's it, it then breaks on the third line with the following error

Error: [2] simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> <?xm
The rest of the XML file is shown.

I have turned on allow_url_fopen and allow_url_include in my settings but still nothing.
I've tried multiple feeds that all end up with the same result?

I'm going mad here

Answer Source

simplexml_load_file() interprets an XML file (either a file on your disk or a URL) into an object. What you have in $feed is a string.

You have two options:

  • Use file_get_contents() to get the XML feed as a string, and use e simplexml_load_string():

    $feed = file_get_contents('...');
    $items = simplexml_load_string($feed);
  • Load the XML feed directly using simplexml_load_file():

    $items = simplexml_load_file('...');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download