doutou7740 2011-12-11 01:56
浏览 48
已采纳

Zend_Feed:从Feed获取图片网址

What is the correct way to go after items more than one level deep with the Zend_Feed library?

How should the second to last line of code read?

require_once 'Zend/Feed/Rss.php';

$url = 'http://leo.am/podcasts/twit';
$c = new Zend_Feed_Rss($url);
$iu = $c->image()->url();
echo $iu;
  • 写回答

2条回答 默认 最新

  • douduan1953 2011-12-11 07:12
    关注

    This was quite interesting!

    Your RSS-Feed has two image-tags:

    <image>
        <url>http://leoville.tv/podcasts/coverart/twit144audio.jpg</url>
        <!-- ... more stuff ... -->
    </image>
    <!-- ... more stuff ... -->
    <itunes:image href="http://leoville.tv/podcasts/coverart/twit600audio.jpg"/>
    

    The first image is in the default namespace and has child nodes, this is probably the one you want. The second is apparently iTunes specific and therefore in the itunes namespace and the url is in the href attribute.

    The Problem is that $c->image returns both elements in an array. I'm not sure if it's a bug or if it works as intended.

    So you have to loop through every element and see if it's from itunes or from rss. This code returns both the itunes and the rss image url in an array as long as they are defined. If none is found the array is empty:

    require_once 'Zend/Feed/Rss.php';
    
    // iTunes Namespace URI
    define('RSS_NS_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
    
    function getImageFromElement(Zend_Feed_Element $elem) {
        $result = array();
        $elemDOM = $elem->getDOM();
        if(RSS_NS_ITUNES == $elemDOM->namespaceURI) {
            $result["itunes"] = $elemDOM->getAttribute('href');
        } else if("" == $elemDOM->namespaceURI) {
            $url = $elem->url();
            if($url)
                $result["rss"] = $url;
        }
    
        return $result;
    }
    
    function getFeedImages(Zend_Feed_Rss $feed) {
        $imgContent = $feed->image;
        $result = array();
    
        if(is_array($imgContent)) {
            // Multiple elements returned
            // check every element for valid URLs
            foreach($imgContent as $i) {
                $result = array_merge($result,
                    getImageFromElement($i));
            }
        } else if($imgContent instanceof Zend_Feed_Element) {
            // Check the one element we've got
            $result = getImageFromElement($imgContent);
        }
    
        return $result;
    }
    
    
    // usage:
    $url = 'http://leo.am/podcasts/twit';
    $c = new Zend_Feed_Rss($url);
    $imageUrls = getFeedImages($c);
    var_dump($imageUrls);
    

    The output looks like this:

    array(2) {
      ["rss"]=>
      string(53) "http://leoville.tv/podcasts/coverart/twit144audio.jpg"
      ["itunes"]=>
      string(53) "http://leoville.tv/podcasts/coverart/twit600audio.jpg"
    }
    

    Edit: Well there is a concise alternative if you don't care about the iTunes URL:

    $url = 'http://leo.am/podcasts/twit';
    $c = new Zend_Feed_Rss($url);
    $imgNoNs=":image";
    echo $c->$imgNoNs->url();
    

    Output:

    http://leoville.tv/podcasts/coverart/twit144audio.jpg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试