Answer the question
In order to leave comments, you need to log in
How to sum views of all videos in a Youtube playlist?
https://gdata.youtube.com/feeds/api/playlists/8BCDD04DE8F771B2?v=2&alt=json
<?php
$youtube_view_count = json_decode(file_get_contents('https://gdata.youtube.com/feeds/api/playlists/PL7FC6D85A4AE63E21'))->entry->{'yt:statistics'}->viewCount;
$elem = new SimpleXMLElement($youtube_view_count);
$a = viewCount;
foreach ($elem as $a){
printf($a->count());
}
?>
Answer the question
In order to leave comments, you need to log in
You get JSON, and then you try to parse it all as XML. That's why it doesn't come out.
A bit of a crutch, but something like this:
$playlist = file_get_contents('https://gdata.youtube.com/feeds/api/playlists/8BCDD04DE8F771B2?v=2&alt=json');
$playlist = str_replace('yt$statistics', 'yt_statistics', $playlist);
$playlist = json_decode($playlist);
$entries = $playlist->feed->entry;
$views = 0;
foreach ((array)$entries as $entry) {
$views += $entry->yt_statistics->viewCount;
}
echo $views;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question