Wednesday, July 21, 2010

Processing media:thumbnail in RSS feeds with php

Today I am creating a php file that will process various news RSS feeds.

The typical story RSS item looks like this:

<title>Mini-stroke care 'risking lives'</title>
<description>Many patients at high risk of stroke are not getting the specialist treatment they need, an audit finds.</description>
<link>http://www.bbc.co.uk/news/health-10713946</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/health-10713946</guid>
<pubDate>Wed, 21 Jul 2010 23:20:34 +0000</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/48443000/jpg/_48443005_stroke1.jpg"/>
</item>


Problem here is trying to get the url attribute from media:thumbnail.

You can not simply call:
$item->media:thumbnail
or
$item['media:thumbnail']

The solution that I found works best simply loads the xml file as a string, then find and replace 'media:thumbnail' with a correctly formatted 'thumbnail' and lastly convert it back to xml with simplexml_load_string:


$xSource = 'http://feeds.bbci.co.uk/news/rss.xml';
$xsourcefile = file_get_contents( $xSource );

$xsourcefile = str_replace("media:thumbnail","thumbnail",$xsourcefile);
$xml = simplexml_load_string( $xsourcefile );
echo $row['xtitle'] . '<BR>';

foreach ($xml->channel->item as $item) {
echo ':' . $item->title . '<BR>';
echo ':' . $item->thumbnail['url'] . '<BR>';
}


Not nearly as easy as I could find but so far the best solution I could come up with. If anyone finds a better solution, please post it here.

No comments: