How to count the total number of views from a youtube channel?

荒凉一梦 提交于 2020-01-07 02:15:27

问题


I want to get the total amount of views (all videos) from a channel on youtube using the Youtube API in PHP. I didn't found any method to do that. Does anyone have en idea ? Thanks in advance for your help.


回答1:


You can use the new YouTube Analytics API

https://developers.google.com/youtube/analytics/v1/available_reports

you can modify the code of the sample application to call the api in the client side:

https://developers.google.com/youtube/analytics/v1/sample-application

and do something like this to get the number of views per day:

var request = gapi.client.youtubeAnalytics.reports.query({
      // Convert dates to YYYY-MM-DD strings for start-date and end-date parameters.
      'start-date': formatDateString(lastWeek),
      'end-date': formatDateString(today),
      // Identify channel for which you're retrieving data.
      ids: 'channel==' + channelId,
      dimensions: 'day',
      metrics: 'views'
    });



回答2:


This is the code (don't forget to rename yourUserName with your YouTube username):

$xdoc = new DomDocument;
$xdoc->Load('http://gdata.youtube.com/feeds/api/users/yourUserName');
$ytstat = $xdoc->getElementsByTagName('statistics')->item(0);
$total_views = $ytstat->getAttribute(totalUploadViews);
echo $total_views;


来源:https://stackoverflow.com/questions/13566343/how-to-count-the-total-number-of-views-from-a-youtube-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!