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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀