我想知道如何获取频道观看的总数。我一直在搜索youtube API,但似乎找不到。
非常感谢您的帮助。
谢谢! :)
答案 0 :(得分:9)
您需要使用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,则为相同的数字。
<强> 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;
}
}
答案 1 :(得分:2)
https://developers.google.com/youtube/v3/docs/channels/list
答案 2 :(得分:0)
此链接提供范围内的YouTube频道报告。但是当与谷歌php库一起使用时,它会被“用户登录需要错误!”
所吸引有没有办法可以像在v3中那样访问youtube V3中的频道分析报告