RSS Namespaces using Zend_Feed_Reader

心已入冬 提交于 2019-12-03 21:11:25

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?

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

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

And voila, there are your thumbnails.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!