Checking Valid RSS feed URL
Asked Answered
H

3

6

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?

Hyson answered 16/9, 2011 at 5:11 Comment(2)
Which version of Simplepie are you using?Mavismavra
@Mavismavra I'm using ver 1.2Hyson
M
7

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.

Mavismavra answered 16/9, 2011 at 6:46 Comment(0)
C
0

Here seems to be ready-to-use function: http://www.sitepoint.com/forums/showthread.php?555763-Validating-an-RSS-Feed-with-PHP&p=3865285&viewfull=1#post3865285

So you just call

$rssvalid = validateFeed("http://yourUrlHere.com");
if($rssvalid == true){
print"Yes, it´s valid!";
} else {
print"Sorry, it´s not valid!";
}
Chloromycetin answered 16/9, 2011 at 5:20 Comment(1)
I have tried this.. The problem is that this validateFeed function strictly examine the validness of Feed. Some feeds are parsable through simplepie even though the validateFeed says it is invalid.Hyson
H
0

This is what I have done.

if(strpos(file_get_contents($feed_url),'<?xml')===false) {
    //remove this $feed_url from the Feed List
    return;
}

This resolved basic problem What I had.

Hyson answered 16/9, 2011 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.