0

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.

user1364743
  • 5,283
  • 6
  • 51
  • 90

2 Answers2

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'
    });
Matias Molinas
  • 2,246
  • 15
  • 11
0

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;
ljn
  • 27
  • 2
  • 8