2014-09-02 56 views

回答

7

您需要使用API​​的Channel.list。通道的總視圖在部分statistics中。

您需要頻道名稱或頻道ID。如果您想要頻道ID,但只有頻道名稱,則可以使用此app獲取該頻道的YouTube ID。

結果的形式是:

{ 
"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" 
    } 
    } 
] 
} 

信道的總的看法是在部分[items"][0]["statistics"]["viewCount"]

對於這種信道,觀看次數是:5 861 117中,相同的編號,如果你看通道https://www.youtube.com/user/Vecci87/about

LIVE EXAMPLE

編輯

您可以使用Youtube API Analytics。重要信息,您需要成爲YouTube帳戶的所有者,此方法需要使用Oauth2進行身份驗證。 我給你一個基本的例子,我定義了兩個日期:今天和過去的一天。我將metrics設置爲view,將dimension設置爲day,以獲得每天的觀看次數。 最後,我添加所有這些值。

$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']); 
} 

完整代碼例如:

<?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; 
    } 
} 
+0

謝謝您的幫助,訪問YouTube的V3通道分析報告一樣等等。這是對我很大的幫助。另一個後續問題。是否有可能在特定的日期範圍內獲得頻道總視圖? – PinoyStackOverflower 2014-09-02 15:10:49

+0

@PinoyStackOverflower nop,除非您使用YouTube Analytics API但您需要進行身份驗證,否則這是不可能的,您需要成爲頻道的擁有者 – mpgn 2014-09-02 16:20:39

+0

是的,其實,t帽子好,請你給我舉個例子。謝謝:) – PinoyStackOverflower 2014-09-03 06:03:46

0
相關問題