I'm having some problems with parsing RSS feeds with Zend_Feed_Reader, specifically when a RSS namespace is being used.
The feed I'm trying to parse is the BBC News feed (http://feeds.bbci.co.uk/news/rss.xml) which includes the following:
<item>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/55800000/jpg/_55800088_013076641-1.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/55807000/jpg/_55807247_013074606-1.jpg"/>
</item>
The code I'm using to parse the other items in <item> is as such:
$feed = Zend_Feed_Reader::import('http://feeds.bbci.co.uk/news/rss.xml');
foreach($feed as $item)
{
echo $item->getTitle();
echo $item->getDescription();
// etc
}
However, using $item->getMedia(), $item->getMedia('thumbnail'), $item->{'media:thumbnail'} or $item->{'media:thumbnail'}() doesn't work.
I also tried writing my own extension (using this as a guide):
class Zend_Feed_Reader_Extension_Media_Entry extends Zend_Feed_Reader_Extension_EntryAbstract
{
public function getThumbnail()
{
if(isset($this->_data['thumbnail']))
return $this->_data['thumbnail'];
$thumbnail = $this->_xpath->evaluate(
'string(' . $this->getXpathPrefix() . '/media:thumbnail)'
);
if(!$thumbnail)
$thumbnail = null;
$this->_data['thumbnail'] = $thumbnail;
return $this->_data['thumbnail'];
}
protected function _registerNamespaces()
{
$this->_xpath->registerNamespace('media', 'http://search.yahoo.com/mrss');
}
}
And then doing all the appropriate extension registering (Zend_Feed_Reader::registerExtension('media');) returns a null when running $item->getThumbnail().
Does anyone have any ideas?
I also did follow the guide at Zend.com and found out that there is a problem with the first code block, the arguments for
Zend_Feed_Reader::addPrefixPathare in incorrect order, and has to be like this:I assume you registered your extension this way?
That done, and with a custom extension I continued. Using the complete example from Zend.com just gave me an empty string. After trying some different notations for getting to the namespace like double colons
::, brackets[]and even@symbols I almost gave up.And then something popped into mind; what if I try to fetch my XPath without a
string()wrap, and so I did (I used your code to make it even clearer):And voila, there are your thumbnails.