【发布时间】:2014-09-02 08:56:30
【问题描述】:
我想知道如何获取频道观看总数。我一直在搜索整个 youtube API,但似乎找不到。
您的帮助将不胜感激。
谢谢! :)
【问题讨论】:
标签: php youtube youtube-api
我想知道如何获取频道观看总数。我一直在搜索整个 youtube API,但似乎找不到。
您的帮助将不胜感激。
谢谢! :)
【问题讨论】:
标签: php youtube youtube-api
您需要使用 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"]部分中
对于该频道,viewCount 为:5 861 117,如果您查看频道https://www.youtube.com/user/Vecci87/about,则为相同的数字。
编辑
您可以使用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;
}
}
【讨论】:
https://developers.google.com/youtube/v3/docs/channels/list
【讨论】:
此链接提供范围内的 youtube 频道报告。但是当与 google php 库一起使用时,它会陷入“需要用户登录错误!”
有什么方法可以访问 youtube V3 中的频道分析报告,就像在 v3 中一样
【讨论】: