2017-03-07 36 views
1

如何訪問此XML文件中最新條目的視圖?XML文件的PHP訪問值

https://www.youtube.com/feeds/videos.xml?channel_id=UCeNdjxcrWI5B1-j8mG4Nm7g

這是我的嘗試,但它不工作:

$xml = simplexml_load_file(sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel_id)); 
      foreach ($xml->entry as $entry) { 
       if (!empty($entry->children('yt', true)->videoId[0])){ 
        $views = $entry->children('group')->children('community')->statistics['views']; 
        echo $views;}} 
+1

你可以發佈你的代碼的其餘部分? – FatBoyXPC

+0

現在應該改變! – BlackWolve

回答

1
<?php 

$url = 'https://www.youtube.com/feeds/videos.xml?channel_id=UCeNdjxcrWI5B1-j8mG4Nm7g'; 
$xml = new SimpleXMLElement($url, null, true); 
print_r($xml->entry->children('media', true)->group->community->statistics->attributes()->views); 

希望這有助於;)

+0

它的工作原理!非常感謝。 – BlackWolve

+0

mark as answer and upvote ...繼續編碼:) –