YouTube Analytics API php Samples

后端 未结 3 1349
别跟我提以往
别跟我提以往 2021-01-06 13:04

I want to make this YouTube Analytics Request with the PHP Client Libary

https://www.googleapis.com/youtube/analytics/v1/reports  
?ids=channel==CHANNELID
         


        
3条回答
  •  生来不讨喜
    2021-01-06 13:26

    After authorizing and/or refreshing tokens, in 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];
    }
    

    EDIT: made it so that, to get results, you can echo $api_response['metric you want'].

提交回复
热议问题