0

I'm working with the youtube v3 api. After a few tests, I realized that I need some help. When I'm trying to display the xml content, I'm just getting null values. Can anybody help me?

If you want to see the xml:

https://www.youtube.com/feeds/videos.xml?channel_id=$channelid

And my code is:

$xml=simplexml_load_file("videos.xml");
foreach($xml as $content) { 
     echo $content->title . "<br>"; 
     echo $content->link['href'] . "<br>"; 

}

Xml that I want to display:

<entry>
Video ID <yt:videoId>Q4vSZA_8kYY</yt:videoId>
Video title <title>¡Trailer del canal! CBPrductions</title>

Upload date <published>2016-01-14T07:37:03+00:00</published>

<media:group>
Description <media:description>
LIKE PORQUE LO DIGO YO _ Suscribete!: https://www.youtube.com/user/SpanishCBProductions Dale a LIKE a mi página Facebook: https://www.facebook.com/SpanishCBProductions Sigueme en TWITTER!: https://twitter.com/CcristianN3 Y en mi poco sexy INSTAGRAM: http://instagram.com/ccristiann3/
</media:description>
</media:group>
</entry>
lluiscab
  • 55
  • 1
  • 6

1 Answers1

2

I think you can register the namespace and use xpath. Then for the 'media' and the 'yt' you can get the children by passing the namespace.

If you want to display the first entry:

$url = 'https://www.youtube.com/feeds/videos.xml?channel_id=UCRGn72Qu0KTtI_ujNxRr3Fg';
$xml = simplexml_load_file($url);
$ns = $xml->getDocNamespaces(true);
$xml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$elements = $xml->xpath('//a:entry');
$content = $elements[0];

$yt = $content->children('http://www.youtube.com/xml/schemas/2015');
$media = $content->children('http://search.yahoo.com/mrss/');
echo "Video ID: " . $yt->videoId . "<br>";
echo "Video title: " . $content->title . "<br>";
echo "Upload date: " . $content->published . "<br>";
echo "Description: " .$media->group->description . "<br>";

If you want to display the information from all the entries, you can use:

foreach ($elements as $content) {
    $yt = $content->children('http://www.youtube.com/xml/schemas/2015');
    $media = $content->children('http://search.yahoo.com/mrss/');
    echo "Video ID: " . $yt->videoId . "<br>";
    echo "Video title: " . $content->title . "<br>";
    echo "Upload date: " . $content->published . "<br>";
    echo "Description: " . $media->group->description . "<br>";
    echo "<br>";
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70