Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a URL is a valid RSS feed in PHP

Tags:

php

rss

I have the following code:

    function parse() {
        $content = file_get_contents($this->feed);        
        $rss = new SimpleXmlElement($content);
        $rss_split = array();
        $i = 0;
        foreach ($rss->channel->item as $item) {
            $title = (string) $item->title; // Title
            $link = (string) $item->link; // Url Link            
            $content = $item->children('content', true)->encoded;
            preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
            $image = substr($image['src'], 0, strpos($image['src'], '"'));
            $rss_split[$i]['title'] = $title;
            $rss_split[$i]['link'] = $link;
            $rss_split[$i]['image'] = $image;
            $i++;
        }
        return $rss_split;
    }

Here, $this->feed contains the RSS feed's URL. The problem is I do not know how to validate the URL to be sure it is a valid RSS feed.

like image 897
J.K.A. Avatar asked Oct 08 '12 05:10

J.K.A.


People also ask

What is RSS in URL?

RSS, in full really simple syndication, formerly called RDF site summary or rich site summary, format used to provide subscribers with new content from frequently updated websites. Related Topics: website.


2 Answers

To verify that it is XML:

function parse()
{
    $content = file_get_contents($this->feed); 
    try { $rss = new SimpleXmlElement($content); }
    catch(Exception $e){ /* the data provided is not valid XML */ return false; }
    // rest of your function goes here

Once you have verified that it is XML you have a couple of options:

  1. you could check to make sure isset($rss->channel->item) existed and $rss->channel->item->count() > 0.
  2. You can use count($rss->xpath(/channel/item)) > 0.

I'd use xpath, personally as I find it a little more obvious when reading the code.


SIDE NOTE:

Seriously? You've already got XML object. Why are you using RegEx?

Don't do this:

preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);

When this is a valid option:

$g = $item->xpath('//img'); $g[0]->attributes()->src;
like image 75
cwallenpoole Avatar answered Sep 28 '22 22:09

cwallenpoole


May this will helpfull to you.

?php

function validateFeed( $sFeedURL )
{

$sValidator = 'http://feedvalidator.org/check.cgi?url=';

if( $sValidationResponse = @file_get_contents($sValidator . urlencode($sFeedURL)) )
{
    if( stristr( $sValidationResponse , 'This is a valid RSS feed' ) !== false )
    {
        return true;
    }
    else
    {
        return false;
    }
}
else
{
    return false;
}
}

?>
like image 44
Amrish Prajapati Avatar answered Sep 28 '22 21:09

Amrish Prajapati