YouTube API v3 PHP gets total channel views

I would like to know how to get the total channel views. I have searched all youtube API but cannot find it.

Your help would be greatly appreciated.

Thank!:)

+3


source to share


3 answers


You need to use the Channel.list API. The general view of the channel is in the part statistics

.

You need the channel name or channel ID. If you want a channel id but you only have the channel name, you can use this app to get the YouTube channel id.

Result form:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"gMjDJfS6nsym0T-NKCXALC_u_rM/0FiX4yi2JggRgndNH8LVUqGkBEs\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "kind": "youtube#channel",
   "etag": "\"gMjDJfS6nsym0T-NKCXALC_u_rM/ch89JvwOeEbWio2fOHY7sxE7XCc\"",
   "id": "UCMGgBRBiijmpgL3xNuiDVOQ",
   "statistics": {
    "viewCount": "5861117",
    "commentCount": "275",
    "subscriberCount": "40674",
    "hiddenSubscriberCount": false,
    "videoCount": "29"
   }
  }
 ]
}

      

General view of the canal is in part [items"][0]["statistics"]["viewCount"]

For this channel, viewCount: 5 861 117, same number if you look at channel https://www.youtube.com/user/Vecci87/about .



LIVE EXAMPLE

EDIT

You can use Youtube API Analytics . Important information, you must be the owner of a YouTube account, this method requires authentication with Oauth2. I gave you a basic example, I define two dates: today and last day. I set metrics

in view

and dimension

in day

, to have an idea for every day. Finally, I add all of these values.

$today = date("Y-m-d");
$datePast = date('Y-m-d', strtotime("-".$period." day"));
try {
    $activitiesView = $youtube->reports->query('channel=='.$idde.'', $datePast , $today, 'views', array('dimensions' => 'day'));
} catch(Google_ServiceException $e) { }

$average = 0;
if(isset($activitiesView['rows'])) {
    foreach ($activitiesView['rows'] as $value) {
        $average += $value[1];
    }   
    $average = $average/count($activitiesView['rows']);
}

      

Complete code example:

<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');

$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setRedirectUri('REDIRECT_URI');
$client->setDeveloperKey('YOUR_DEV_KEY');

$youtube = new Google_YouTubeAnalyticsService($client);
$service = new Google_YouTubeService($client);
$auth2 = new Google_Oauth2Service($client);

if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));  
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {

    /***************USER STATS********************/
    $today = date("Y-m-d");
    $datePast = date('Y-m-d', strtotime("-".$period." day"));
    try {
        $activitiesView = $youtube->reports->query('channel=='.$idde.'', $datePast , $today, 'views', array('dimensions' => 'day'));
    } catch(Google_ServiceException $e) { }

    $average = 0;
    if(isset($activitiesView['rows'])) {
        foreach ($activitiesView['rows'] as $value) {
            $average += $value[1];
        }   
        $average = $average/count($activitiesView['rows']);
    }


    /***************USER STATS********************/

  $_SESSION['token'] = $client->getAccessToken();
} else {

    $authUrl = $client->createAuthUrl();
    //simple verification
    if(strpos($RedirectUri, "redirect_uri") !== false) {
        header('Location: error.php');
        exit;
    }
}

      

+7


source


  • Go to next url

https://developers.google.com/youtube/v3/docs/channels/list

  1. Use the API to call this method on live data and view the API request and response Sample Images
  2. Execute API and see response


enter image description here

  1. In the statistics section you will find information about what you are looking for.
+2


source


0


source







All Articles