【问题标题】:YouTube API v3 PHP get total number of views in a channelYouTube API v3 PHP 获取频道中的总观看次数
【发布时间】:2014-09-02 08:56:30
【问题描述】:

我想知道如何获取频道观看总数。我一直在搜索整个 youtube API,但似乎找不到。

您的帮助将不胜感激。

谢谢! :)

【问题讨论】:

    标签: php youtube youtube-api


    【解决方案1】:

    您需要使用 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;
        }
    }
    

    【讨论】:

    • 感谢您的帮助。这对我帮助很大。另一个后续问题..是否可以在特定日期范围内获得频道的总体视图?
    • @PinoyStackOverflower nop,这是不可能的,除非您使用 YouTube Analytics API 但您需要经过身份验证,并且您需要成为频道的所有者。
    • 是的,其实,没关系,你能给我举个例子吗?谢谢:)
    • 有关如何获取特定日期范围内的总频道观看次数的代码示例。您说除非我们使用 YouTube Analytics API,否则这是可能的。 :)
    • 谢谢!非常感谢! :)
    【解决方案2】:
    1. 转到以下网址

    https://developers.google.com/youtube/v3/docs/channels/list

    1. 使用 API Explorer 对实时数据调用此方法并查看 API 请求和响应
    2. 执行 API 并查看响应

    1. 查看统计信息部分,您可以找到所需的信息

    【讨论】:

      【解决方案3】:

      此链接提供范围内的 youtube 频道报告。但是当与 google php 库一起使用时,它会陷入“需要用户登录错误!”

      https://developers.google.com/apis-explorer/#p/youtubeAnalytics/v1/youtubeAnalytics.reports.query?ids=channel%253D%253DMINE&start-date=2014-05-01&end-date=2014-06-30&metrics=views&dimensions=day&_h=3&

      有什么方法可以访问 youtube V3 中的频道分析报告,就像在 v3 中一样

      http://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list?part=statistics&id=UCMGgBRBiijmpgL3xNuiDVOQ&_h=10&

      【讨论】:

        猜你喜欢
        • 2013-11-16
        • 1970-01-01
        • 2012-06-11
        • 2023-03-17
        • 2015-08-30
        • 1970-01-01
        • 2014-11-23
        • 1970-01-01
        • 2015-07-24
        相关资源
        最近更新 更多