B. Dionys
B. Dionys

Reputation: 916

XML RSS image displaying in PHP

Im trying to pull an image URL out of an RSS link.

Here's an image of one of the items inside there.

enter image description here

In the image there is an tag media:thumbnail

I am using this for the title, link and discription:

$item_title = $item->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link = $item->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $item->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

However I cannot seem to pull out the media:thumbnail.

I have tried the same as the other one's

$item_img = $item->getElementsByTagName('media:thumbnail')->item(0)->childNodes->item(0)->nodeValue;

But this gives me this error: enter image description here

Line 24 is the line that I tried. but I also tried using .attribute(url) but this also doesnt return a thing.

I hope someone can help me with this, its been 4 hours since I got this problem but after a lot of try and error there wasnt a single time I got the URL back.

Thanks in advance, and happy coding.

Upvotes: 1

Views: 601

Answers (1)

Decent Dabbler
Decent Dabbler

Reputation: 22783

The media: part on elements in your XML, is called a namespace prefix and represents a namespace URI. You should be able to find this URI as the value of a xmlns:media attribute on one of the media: element's ancestors. If you can't find that, you should be able to use the URI value of the xmlns attribute on the element itself (if applicable, which in this case it is).

In order to fetch certain elements from a certain namespace you should then use that namespace's URI and call DOMDocument::getElementsByTagNameNS() instead of DOMDocument::getElementsByTagName(), like so:

$item_img = $item->getElementsByTagNameNS('the namespace URI you found','thumbnail')
                 ->item(0)
                 ->childNodes
                 ->item(0)->nodeValue;

If you don't care about what namespace a particular element is defined in, you could just keep using DOMDocument::getElementsByTagName(), but leave out the namespace prefix:

$item_img = $item->getElementsByTagName('thumbnail')
                 ->item(0)
                 ->childNodes
                 ->item(0)->nodeValue;

Be aware though that this will fetch all <thumbnail> elements, no matter what namespace they are in.

However, in the sample XML you've shown1, the <media:thumbnail> element does not have any child nodes, so the above examples of your code that I adjusted will still fail.

Lastly, .attribute(url) is just invalid syntax. It should be ->getAttribute('url'), of course.

So, to wrap it up, you should be able to get the url attribute of the first <thumbnail> of a particular namespace with:

$item_img = $item->getElementsByTagNameNS('the namespace URI you found','thumbnail')
                 ->item(0)->getAttribute('url');

or:

$item_img = $item->getElementsByTagName('thumbnail')
                 ->item(0)->getAttribute('url');

if you don't care about the namespace of the element.


1) Please post actual sample XML code next time, instead of an image.

Upvotes: 1

Related Questions