向YouTube API PHP发送请求时出现奇怪错误

kr98yfug  于 2023-06-20  发布在  PHP
关注(0)|答案(2)|浏览(102)

我目前正在开发一个后端处理程序,它可以从PHP上的YouTube Analytics API请求频道分析信息。由于某种原因,我不断收到一个奇怪的错误消息:

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 ...

所以我假设错误与$query相关,输入应该是数组类型,所以我这样做了:

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 Google\Exception: (query) unknown parameter: '0'

但是,正如你所看到的,仍然存在一个错误。对于第二个,我假设PHP中的数组在技术上是顺序Map,这就是为什么它对'0'进行协调,但我仍然感到困惑,如果它不能处理它,为什么它会要求一个数组。
关于我的代码的更多上下文,我使用了Google API的PHP客户端库,这是我通过composer require google/apiclient:^2.0获得的。下面是我的整个代码文件,我在其中示例化了所有对象:

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]);
    }
}

将感谢任何帮助的人谁有经验的互动与YouTube分析API使用PHP!谢谢!

6yt4nkrj

6yt4nkrj1#

我不确定这是否有效,但是如果你使用一个关联数组而不是一个常规数组呢?

$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
     ];

如果它工作了,你想使用compact,只是为了更短的语法:

$params = compact('id', 'start_date', 'end_date', 'metric', 'opt_params');
rbl8hiat

rbl8hiat2#

参数数组必须包含键值。你应该像这样发送一个参数数组:

$client->setAccessToken($_SESSION['access_token']);
$youtube = new Google_Service_YouTubeAnalytics($client);

$queryParams = array(
                    'startDate' => '2016-05-01',
                    'endDate' => '2016-06-30',
                    'metrics' => 'views',
                    'ids' => 'channel==MINE'
                    );

$report = $youtube->reports->query( $queryParams );

有关每个参数需要如何格式化的更多信息,请检查参数的文档。

相关问题