1

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.

1
  • 1
    For completeness can you perhaps post an actual textual code snippet of the XML, in stead of an image (certainly next time you post another question)? It makes creating a quick test much easier for people that want to help you. Thanks in advance! Commented Jun 8, 2017 at 16:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A more perfect answer doesnt exist. Thank you it worked. and also thanks for the explanation about what I did wrong!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.