Search code examples
phprssitunes

How to get iTunes-specific child nodes of RSS feeds?


I'm trying to process an RSS feed using PHP and there are some tags such as 'itunes:image' which I need to process. The code I'm using is below and for some reason these elements are not returning any value. The output is length is 0.

How can I read these tags and get their attributes?

$f = $_REQUEST['feed'];
$feed = new DOMDocument();
$feed->load($f);
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

foreach($items as $key => $item) 
{
    $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
    $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
    $description = $item->getElementsByTagName('description')->item(0)->textContent; // textContent

    $arrt = $item->getElementsByTagName('itunes:image');
    print_r($arrt);
}

Solution

  • getElementsByTagName is specified by DOM, and PHP is just following that. It doesn't consider namespaces. Instead, use getElementsByTagNameNS, which requires the full namespace URI (not the prefix). This appears to be http://www.itunes.com/dtds/podcast-1.0.dtd*. So:

        $img = $item->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image');
        // Set preemptive fallback, then set value if check passes
        urlImage = '';
        if ($img) {
          $urlImage = $img->getAttribute('href');
        }
    

    Or put the namespace in a constant.

    You might be able to get away with simply removing the prefix and getting all image tags of any namespace with getElementsByTagName.

    Make sure to check whether a given item has an itunes:image element at all (example now given); in the example podcast, some don't, and I suspect that was also giving you trouble. (If there's no href attribute, getAttribute will return either null or an empty string per the DOM spec without erroring out.)

    *In case you're wondering, there is no actual DTD file hosted at that location, and there hasn't been for about ten years.