使用 Zend_Feed_Reader 的 RSS 命名空间 [英] RSS Namespaces using Zend_Feed_Reader

查看:21
本文介绍了使用 Zend_Feed_Reader 的 RSS 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Zend_Feed_Reader 解析 RSS 提要时遇到一些问题,特别是在使用 RSS 命名空间时.

I'm having some problems with parsing RSS feeds with Zend_Feed_Reader, specifically when a RSS namespace is being used.

我试图解析的提要是 BBC 新闻提要 (http://feeds.bbci.co.uk/news/rss.xml),其中包括以下内容:

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
}

然而,使用 $item->getMedia(), $item->getMedia('thumbnail'), $item->{'media:thumbnail'}$item->{'media:thumbnail'}() 不起作用.

However, using $item->getMedia(), $item->getMedia('thumbnail'), $item->{'media:thumbnail'} or $item->{'media:thumbnail'}() doesn't work.

我也尝试编写自己的扩展程序(使用 this as a guide):

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');
    }
}

然后进行所有适当的扩展注册 (Zend_Feed_Reader::registerExtension('media');) 在运行 $item->getThumbnail() 时返回一个 null.

And then doing all the appropriate extension registering (Zend_Feed_Reader::registerExtension('media');) returns a null when running $item->getThumbnail().

有人有什么想法吗?

推荐答案

我也确实遵循了 Zend.com 发现第一个代码块有问题,Zend_Feed_Reader::addPrefixPath 的参数顺序不正确,必须像这样:

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::addPrefixPath are in incorrect order, and has to be like this:

if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
    Zend_Feed_Reader::addPrefixPath(
        'My_FeedReader_Extension', '/path/to/My/FeedReader/Extension'
    );
    Zend_Feed_Reader::registerExtension('JungleBooks');
}

我假设您以这种方式注册了您的扩展程序?

I assume you registered your extension this way?

完成,我继续使用自定义扩展.使用 Zend.com 的完整示例只是给了我一个空字符串.在尝试了一些不同的符号来访问命名空间后,例如双冒号 ::、方括号 [] 甚至 @ 符号,我几乎放弃了.

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.

然后我想到了一些事情;如果我尝试在没有 string() 换行的情况下获取我的 XPath 会怎样,所以我做到了(我使用您的代码使其更加清晰):

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):

class Zend_Feed_Reader_Extension_Media_Entry 
    extends Zend_Feed_Reader_Extension_EntryAbstract
{
    public function getThumbnails()
    {
        if(isset($this->_data['thumbnails'])){
            return $this->_data['thumbnails'];
        }

        $thumbnail_list = $this->_xpath->evaluate(
            $this->getXpathPrefix() . '/media:thumbnail'
        );
        $thumbnails = array();

        // According to your XML sample there are multiple thumbnails per item, so we're looping through them and adding them to a simple array
        foreach($thumbnail_list as $_thumbnail_element){
            array_push($thumbnails, array(
                'url'    => $_thumbnail_element->getAttribute('url'),
                'width'  => $_thumbnail_element->getAttribute('width'),
                'height' => $_thumbnail_element->getAttribute('height'),
            ));
        }

        if(!count($thumbnails)){
            $thumbnails = null;
        }

        $this->_data['thumbnails'] = $thumbnails;

        return $this->_data['thumbnails'];
    }

    protected function _registerNamespaces()
    {
        $this->_xpath->registerNamespace('media', 'http://search.yahoo.com/mrss');
    }
}

瞧,这是你的缩略图.

这篇关于使用 Zend_Feed_Reader 的 RSS 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆