1

I'm looking for a way to get the value of the url attribute with the media:thumbnail tag of this RSS feed.

I currently have this code:

//getNamespaces
$ns=$rss->getNamespaces(true);

foreach($rss->entry as $entry) {
    //set children of namespaces
    $yt=$entry->children($ns['yt']);
    $media=$entry->children($ns['media']);
}

But the media element/object for the tag I want is empty.

I have tried using simplexml attributes without success.

vespino
  • 1,714
  • 3
  • 15
  • 28
  • The crucial detail here is that the attribute you are trying to select has no prefix, and therefore according to the XML spec *is not in any namespace*, as [discussed in this question](http://stackoverflow.com/questions/30260452/simplexml-childrens-attributes-behaves-different-with-and-without-namespace?lq=1). – IMSoP Nov 19 '15 at 17:23

1 Answers1

2

I think that you can loop the children of $media and then get the attributes() from the thumbnail.

Maybe this setup can help you:

<?php
$url = "https://www.youtube.com/feeds/videos.xml?user=XLLease";
$rss = simplexml_load_file($url);

//getNamespaces
$ns=$rss->getNamespaces(true);

foreach($rss->entry as $entry) {
    //set children of namespaces
    $yt=$entry->children($ns['yt']);
    $media=$entry->children($ns['media']);
    foreach ($media as $value) {
        $url = $value->thumbnail->attributes()->url->__toString();
    }
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70