2013-12-20 44 views
3

我想使這個YouTube數據分析請求的PHP客戶端Libary的YouTube Analytics(分析)API PHP示例

https://www.googleapis.com/youtube/analytics/v1/reports 
?ids=channel==CHANNELID 
&start-date=STARTDATE 
&end-date=ENDDATE 
&metrics=views, 
estimatedMinutesWatched, 
averageViewDuration, 
comments, 
favoritesAdded, 
favoritesRemoved, 
likes, 
dislikes, 
shares, 
subscribersGained, 
subscribersLost 
&dimensions=7DayTotals 
&fields=rows 
&sort=day 

這可能嗎? 是否有任何PHP代碼示例瞭解如何從API獲取YouTube分析報告? 。

$analytics = new Google_YouTubeAnalyticsService($client); 
// $client is your Google_Client object 

// here we set some params 
$id = 'channel==CHANNELID' 
$start_date = 'YYYY-MM-DD'; 
$end_date = 'YYYY-MM-DD'; 
$optparams = array(
    'dimensions' => '7DayTotals', 
    'sort' => 'day', 
); 

$metrics = array(
    'views', 
    'estimatedMinutesWatched', 
    'averageViewDuration', 
    'comments', 
    'favoritesAdded', 
    'favoritesRemoved', 
    'likes', 
    'dislikes', 
    'shares', 
    'subscribersGained', 
    'subscribersLost' 
); 

$api_response = $metrics; 

// You can only get one metric at a time, so we loop 
foreach ($metrics as $metric) 
{ 
    $api = $analytics->reports->query($id, $start_date, $end_date, $metric, $optparams); 
    if (isset($api['rows'])) $api_response[$metric] = $api['rows'][0][0]; 
} 

編輯:
找不到一個谷歌開發者頁面上(https://developers.google.com/youtube/analytics/v1/code_samples/

感謝

回答

5

您可以通過以下方式添加指標,而不是逐個調用每個指標。

$metrics = 'views,estimatedMinutesWatched,averageViewDuration,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost'; 

您的結果將在文檔中描述。 例子:

$optparams = array(
    'dimensions' => '7DayTotals', 
    'sort' => 'day', 
); 

$metrics = 'views,estimatedMinutesWatched,averageViewDuration,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost'; 

$api_response = $metrics; 
$api = $analytics->reports->query($id, $start_date, $end_date, $metric, $optparams); 

if (isset($api['rows'])) { 
    //Get values in form of multidimensional arrays. 
} 
6

授權和/或刷新令牌,在PHP之後使它因此,爲了獲得結果,您可以回顯$ api_response ['you metric you']。

0

這一天收集逐天分析在過去的30天,然後將其插入到數據庫中。 這裏是代碼:

set_include_path(get_include_path() . PATH_SEPARATOR . '/home/google-api-php-client/src'); 

require_once('Google/autoload.php'); 

require_once 'Google/Client.php'; 
require_once 'Google/Service/YouTube.php'; 

session_start(); 

$client = new Google_Client(); 
$client->setClientId($OAUTH2_CLIENT_ID); 
$client->setClientSecret($OAUTH2_CLIENT_SECRET); 
$client->setAccessType("offline"); 
$client->setScopes(array('https://www.googleapis.com/auth/youtube.force-ssl', 'https://www.googleapis.com/auth/youtubepartner-channel-audit', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly','https://www.googleapis.com/auth/youtubepartner')); 
$client->setDeveloperKey($key); 

$analytics = new Google_Service_YouTubeAnalytics($client); 

$ids = 'channel==' . $channel_url . ''; 
$end_date = date("Y-m-d"); 
$start_date = date('Y-m-d', strtotime("-30 days")); 
$optparams = array(
'dimensions' => 'day', 
); 

$metric = 'views'; 

try{ 

$api = $analytics->reports->query($ids, $start_date, $end_date, $metric, $optparams); 

foreach ($api->rows as $r) { 
    $date = $r[0]; 
    $views = $r[1]; 

$stmt = $db->prepare("INSERT INTO test (date,views,channel_url) VALUES (:date,:views,:channel_url)"); 
$stmt->execute([':date' => $date, ':views' => $views, ':channel_url' => $channel_url]); 
} 
}catch (Google_Service_Exception $e) { 
    echo sprintf('<p>A service error occurred: <code>%s</code></p>', 
    htmlspecialchars($e->getMessage())); 
} 
相關問題