Search code examples
phpnamespacesrss

Get enclosure tag from RSS feed that has colon in tagname using PHP


I am trying to get the image from a RSS feed with an enclosure tag with what I think is a namespace.

<enc:enclosure resource="path/to/image" type="image/jpeg"/>

I can extract all other fields but it seems with the enc:enclosure I am failing..

I have tried this with no success::

'image' => $node->getElementsByTagNameNS($node->lookupNamespaceURI('enc:enclosure'), "resource")->item(0)->nodeValue,
'image' =>  $node->getElementsByTagNameNS("enc:enclosure", "resource")->item(0)->nodeValue,
 etc..

I get all other tags available using::

title' => $node->getElementsByTagName('title')->item(0)->nodeValue,

What would be the correct way to extract that enclosure tag?


Solution

  • When calling lookupNamespaceURI(), you should just pass the prefix, you are passing the whole prefix and node name.

    'image' => $node->getElementsByTagNameNS($node->lookupNamespaceURI('enc'), "enclosure")
                           ->item(0)->getAttribute("resource")