2016-01-29 138 views
-1

我正在使用youtube v3 api。經過幾次測試,我意識到我需要一些幫助。當我試圖顯示XML內容時,我只是得到空值。有誰能夠幫助我?Php - 在表中顯示youtube視頻

如果你想看到XML:

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

而且我的代碼是:

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

}

的XML我想顯示:

<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> 
+0

可能想要把實際的信道ID在鏈接 – thanksd

+0

你可以用戶任何你想要的id,例如= UCBR8-60-B28hp2BmDPdntcQ(YouTube Spotlight) – lluiscab

+0

請將相關部分的XML複製到你的問題 – michi

回答

2

我想你可以註冊命名空間並使用xpath。 然後對於'media'和'yt',你可以通過傳遞命名空間來獲得children

如果你想顯示的第一個條目:

$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>"; 

如果要顯示所有條目的信息,您可以使用:

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>"; 
} 
+0

非常感謝!但是,有一個問題,youtube xml模式,w3.org和yahoo鏈接的「使用」是什麼? – lluiscab

+0

Btw:縮略圖我必須做的:$ media-> group-> thumbnail ['url']? – lluiscab

+0

@lluiscab對於你可以使用的縮略圖:'$ media-> group-> thumbnail-> attributes() - > url - > __ toString()' –