0
votes

I have a class that I use for pulling in different RSS feeds on several different pages of my website.

My problem is when one of the feeds has an issue, (such as a feed going down temporarily), it gives me fatal php errors, therefore breaking my site.

$rawFeed = @file_get_contents("http://www.website.com/feed");
$xml = new SimpleXmlElement($rawFeed);

This is the basic code that I use to pull the rss feeds. The new SimpleXmlElement is what ultimately gives the fatal error.

2

2 Answers

1
votes
libxml_use_internal_errors(true); 
try
{
    $xml = new SimpleXmlElement('http://www.website.com/feed');
} catch(Exception $e) {
    //nothing
}
1
votes

file_get_contents returns false on an error if that's your only concern.

You could try doing something like this:

$rawFeed = @file_get_contents("http://www.website.com/feed");

if ($rawFeed) {
    $xml = new SimpleXmlElement($rawFeed);
} else {
   // Deal with case that the feed wasn't read.
}

You'd probably also want to wrap the new SimpleXmlElement($rawFeed) in a try/catch block and add handling for the case that the SimpleXmlElement throws a parsing exception.