PHP文件的PHP访问值

时间:2017-03-07 22:32:11

标签: php xml youtube

如何在此XML文件中访问最新条目的视图?

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 个答案:

答案 0 :(得分: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);

希望这会有所帮助;)