YouTube Analytics API php示例

mfs*_*ymb 3 php youtube youtube-api youtube-analytics

我想通过PHP Client Libary制作此YouTube Analytics请求

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  
Run Code Online (Sandbox Code Playgroud)

这可能吗?是否有关于如何从API获取YouTube Analytics报告的PHP代码示例?
在Google Developers页面上找不到.(https://developers.google.com/youtube/analytics/v1/code_samples/)

谢谢

tro*_*ytc 7

在PHP中授权和/或刷新令牌之后:

$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];
}
Run Code Online (Sandbox Code Playgroud)

编辑:这样做,以获得结果,你可以回应$ api_response ['你想要的指标'].


小智 6

您可以按以下方式添加指标,而不是逐个调用每个指标.

$metrics = 'views,estimatedMinutesWatched,averageViewDuration,comments,favoritesAdded,favoritesRemoved,likes,dislikes,shares,subscribersGained,subscribersLost';
Run Code Online (Sandbox Code Playgroud)

您的结果将在文档中描述的from中.例:

$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. 
}
Run Code Online (Sandbox Code Playgroud)