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.
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.
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:
isset($rss->channel->item)
existed and $rss->channel->item->count()
> 0.count($rss->xpath(/channel/item)) > 0
.I'd use xpath, personally as I find it a little more obvious when reading the code.
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;
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;
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With