Search code examples
phpzend-frameworkdomzend-feed

Zend_Feed: getting the Image URL from a 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;

Solution

  • 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