2013-07-30 92 views
3

我發現我可以通過收視次數(命中)的YouTube頻道的視頻列表,並限制該鏈接結果如下顯示輸出XML - PHP

http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5

如此當然會返回一些xml Feed代碼。我正在嘗試將視頻列在我的網站上。

我已經試過:

<?php 
    $list = Array(); 
    //get the xml data from youtube 
    $url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5"; 
    $xml = simplexml_load_file($url); 
    //load up the array $list['title'] = $xml->title[0]; 
    $list['title'] = $xml->title[0]; 
    echo $list['title']; 
?> 

到目前爲止,只是給我的標題爲XML的飼料,如果我嘗試$xml->title[1]。它不會返回任何東西。我如何使用該XML feed來列出標題和(href)將它們鏈接到視頻?我試圖從xml源,視頻的標題和URL檢索2件事。

謝謝。

+1

你可以嘗試做一個'的var_dump($ XML)'並給出了結果? 我認爲問題在於你正在尋找的是事實上的子元素。 – MisterJ

回答

3

像這樣:

<?php 
$url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5"; 
$xml = simplexml_load_file($url); 
foreach($xml->entry as $entry){ 
    echo "Title: ".$entry->title."<br />"; 
    echo "Link :".$entry->link['href']."<br />"; 
} 
?> 
+0

這幫助了我自己的問題(http://stackoverflow.com/questions/22880348/cant-access-xml-node-via-xpath-yt-channel-feed)。我很好奇爲什麼'$ xml-> entry'可以工作,但是'$ xml-> xpath('entry')'看起來不會......(返回0個節點)。 – Utkanos

0

文檔根下的第一個標題標籤constains飼料的標題,其中僅存在一個。 要訪問的後續條目,請使用$xml->entry[0]

<?php 
    $list = Array(); 
     // get the xml data from youtube 
     $url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5"; 
     $xml = simplexml_load_file($url); 
     // load up the array $list['entry'] = $xml->title[0]; 
     $entries = $xml->entry; 
//echo preg_replace("feed","fd"file_get_contents($url)); 
    echo $entries[1]->title;//entry[0],entry[1],entry[2]... will work 
     ?>