4

I'm trying to get SimplePie to fail gracefully if one of the feeds I hand it turns out to be unavailable or invalid (due to server issues on the feed provider's end)

The code I've got is this:

$this->feed= new SimplePie();
// Set which feed to process.
$this->feed->set_feed_url('http://my_feed_goes_here'); // Bogus
$this->feed->handle_content_type();

// Run SimplePie.
$this->feed->init();

The problem is, if the feed_url turns out to be invalid, I get the following error as soon as it hits $this->feed->init();

Fatal error: Call to undefined method DOMElement::getLineNo() 

I've looked through the documentation, and I can't see anything about validating. I did see this page about error checking (http://simplepie.org/wiki/reference/simplepie/error) but that only really works if the URL is completely invalid and fails to load. In a case where the URL comes back with a 404, or something else that is not a valid feed, $feed->error is blank.

Isn't there some mechanism built into SimplePie to allow me to see whether I got a valid feed back, so I can fail gracefully if I didn't?

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
dvanhook
  • 253
  • 1
  • 2
  • 10

2 Answers2

2

In SimplePie 1.3.1, ->init() will return false if it can't read or parse the URL, so you might do this:

if (! $feed->init()) {
    // log your error, "return false" or handle the failure some other way
}

Based on my reading of simplepie\library\SimplePie.php, it doesn't generate any exceptions and that's why Try/Catch won't work.

William Turrell
  • 3,227
  • 7
  • 39
  • 57
0

This may not be built into SimplePie, but in your calling PHP you could use a try/catch block:

try {
    $this->feed->init();
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Although depending on the error, this might not work either.

Revent
  • 2,091
  • 2
  • 18
  • 33