Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Valid RSS feed URL

I have decided to use SimplePie to parse RSS and Atom Feeds.

What I want to do is that let people to input RSS and Atom Feeds URL through text fields.

What If they put invalid RSS and Atom Feeds?

I know that invalid Feeds won't be parsed through SimplePie.

But I want to know whether the Feeds are able to be parsed through SimplePie or not.

And through that process, I want to delete those invalid RSS feed URL lists.

Checking the document type, XML or HTML will be a first step to find out the validness.

How can I do that in PHP? or is there other ways to do what I want to do?

like image 518
jwchang Avatar asked Sep 16 '11 05:09

jwchang


1 Answers

To check if Simplepie is able to parse a feed or not, you can just load the feed in question and check for errors:

$feed = new SimplePie();
$feed->set_feed_url('http://example.com/rss');
$feed->init();
$feed->handle_content_type();

if ($feed->error())
{
    // this feed has errors
}

You might want to disable the auto-discovery feature to test specific feed URLs. Additionally you can aquire the feeds data on your own and use set_raw_data instead of set_feed_url.

like image 186
hakre Avatar answered Sep 30 '22 14:09

hakre