使用zend 2 feed解析RSS文件,我无法获得媒体标记

时间:2013-12-18 15:53:11

标签: php zend-framework2

我似乎无法解决这个问题。我想从RSS文件获取媒体:缩略图。使用ZF2; Zend \ Frame a按照手册但我无法从xml文件中获取图像,任何想法都是如此:)

控制器代码:

<?php

namespace RSS\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Feed\Reader as feed;

class IndexController extends AbstractActionController
{

    public function indexAction(){

        try{

            $rss =            feed\Reader::import('http://www.wdcdn.net/rss/presentation/library/client/skunkus/id/cc3d06c1cc3834464aef22836c55d13a');
        }catch (feed\Exception\RuntimeException $e){
            echo "error : " . $e->getMessage();
            exit;
        }

       $channel = array(
            'title'       => $rss->getTitle(),
            'description' => $rss->getDescription(),
            'link'        => $rss->getLink(),
            'items'       => array()
        );

        foreach($rss as $item){
            $channel['items'][] = array(
                'title'       => $item->getTitle(),
                'link'        => $item->getLink(),
                'description' => $item->getDescription(),
               // 'image'       => $item->getImage(),
            );
        }

        return new  ViewModel(array(
            'channel' => $channel
        ));
    }


}

2 个答案:

答案 0 :(得分:4)

<强>您好

谁得到相同的pb我通过向Zend / Feed / Reader / Entry / rss.php添加一个名为getMedia()的新函数解决它,那个代码为谁有更好的想法或更好的代码我会感谢您的帮助:

    public function getMedia()
{
    if (array_key_exists('media', $this->data)) {
        return $this->data['media'];
    }

    $media = null;

    if ($this->getType() == Reader\Reader::TYPE_RSS_20) {
        $nodeList = $this->xpath->query($this->xpathQueryRss . '/media:thumbnail');

        if ($nodeList->length > 0) {
            $media = new \stdClass();
            $media->url    = $nodeList->item(0)->getAttribute('url');

        }
    }
    $this->data['media'] = $media;

    return $this->data['media'];
}

答案 1 :(得分:2)

这是一种无需扩展或修改类的方法:

foreach ($channel as $item) {
    $xmlItem = $item->saveXml();
    $xmlFeed = new \SimpleXMLElement($xmlItem);
    $thumbAttr = $xmlFeed->children('http://search.yahoo.com/mrss/')->thumbnail->attributes();
    $thumbUrl = (string)$thumbAttr['url'];
}