Skip to content
Advertisement

Weird error while sending request to YouTube API PHP

I am currently working on a back-end processing that requests channel analytics info back from YouTube Analytics API on PHP. For some reason, I keep receiving a weird error message in:

foreach ($metrics as $metric) {
$api = $analytics->reports->query($id, $start_date, $end_date, $metric, $optparams);
print('reached');
    foreach ($api->rows as $r) {
        print($r[0]);
        print($r[1]);
    }
}

Fatal error: Uncaught TypeError: array_merge(): Argument #2 must be of type array, string given in ... 

So I assumed that the error is associated with $query and the input should be of type array so I did that:

foreach ($metrics as $metric) {
$params = [$id, $start_date, $end_date, $metric, $optparams];
$api = $analytics->reports->query($params);
print('reached');
    foreach ($api->rows as $r) {
        print($r[0]);
        print($r[1]);
    }
}

Fatal error: Uncaught GoogleException: (query) unknown parameter: '0'

But as you can see an error still remains. For the second one, I am assuming since arrays in PHP are technically order maps, that is why it is concerted about ‘0’ but I am still confused why it would ask for an array if it can’t process it.

For more context regarding my code, I am using Google API’s client library for PHP which I obtained with composer require google/apiclient:^2.0 . Here is my whole code file where I instantiate all the objects:

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  // Set the access token on the client.
  $client->setAccessToken($_SESSION['access_token']);

  // Create an authorized analytics service object.
  $analytics = new Google_Service_YouTubeAnalytics($client);

} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/Analytics_Dashboard/oauth2callbackYouTube.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

// here we set some params
$id = '////////';
$end_date = date("Y-m-d"); 
$start_date = date('Y-m-d', strtotime("-30 days"));
$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)
{
    $params = [$id, $start_date, $end_date, $metric, $optparams];
    $api = $analytics->reports->query($params);
    // if (isset($api['rows'])) $api_response[$metric] = $api['rows'][0][0];
    print('reached');
    foreach ($api->rows as $r) {
        print($r[0]);
        print($r[1]);
    }
}

Would appreciate any help from people who had experience interacting with YouTube Analytics API using PHP! Thanks!

Advertisement

Answer

Im not sure if this will work, but what if you use an associative array instead of a regular one?

    $params = [$id, $start_date, $end_date, $metric, $optparams]; //OLD
    $params = [ 
       'id' => $id, 
       'start_date' => $start_date, 
       'end_date' => $end_date, 
       'metric' => $metric, 
       'opt_params' => $optparams
     ];

In case it worked, you would like to use compact, just for a shorter syntax:

    $params = compact('id', 'start_date', 'end_date', 'metric', 'opt_params');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement