Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

include_once(ABSPATH.WPINC.'/rss.php');
$feed = file_get_contents('http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int');
$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

like image 653
Gavin Mannion Avatar asked Feb 09 '14 16:02

Gavin Mannion


2 Answers

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('...');
    
like image 68
Amal Murali Avatar answered Oct 20 '22 08:10

Amal Murali


You can also load the content with cURL, if file_get_contents insn't enabled on your server.

Example:

$ch = curl_init();  

curl_setopt($ch,CURLOPT_URL,"http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$output = curl_exec($ch);

curl_close($ch);

$items = simplexml_load_string($output);
like image 4
Herlon Aguiar Avatar answered Oct 20 '22 06:10

Herlon Aguiar