【问题标题】:YouTube Analytics API php SamplesYouTube 分析 API php 示例
【发布时间】:2014-01-08 18:59:21
【问题描述】:

我想通过 PHP 客户端库提出这个 YouTube 分析请求

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  

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

谢谢

【问题讨论】:

    标签: php youtube youtube-api youtube-analytics


    【解决方案1】:

    您可以通过以下方式添加指标,而不是逐个调用每个指标。

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

    您的结果将在文档中描述的 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. 
    }
    

    【讨论】:

    • 我该如何添加这个参数:include-historical-channel-data=true as per developers.google.com/youtube/analytics/v1/reference/reports/… ?
    • @pdolinaj 我相信你可以将它添加到$optparams,就像这样$optparams = [ 'dimensions' => '7DayTotals', 'sort' => 'day', 'include-historical-channel-data' => TRUE, ];
    • 要使用$analytics,请执行以下操作:composer require google/apiclient:^2.11; $analytics = new Google\Service\YouTubeAnalytics($client);
    【解决方案2】:

    授权和/或刷新令牌后,在 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];
    }
    

    编辑:这样做是为了获得结果,您可以回显 $api_response['metric you want']。

    【讨论】:

      【解决方案3】:

      这会收集过去 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()));
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-18
        • 2013-11-08
        • 1970-01-01
        • 2018-05-12
        • 2023-04-08
        • 2015-12-30
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多