Php 在 Google Analytics 中调用 reports->batchGet 时停止 运行 (?)

Php stops running (?) when calling reports->batchGet in Google Analytics

我正在使用 google here 提供的基本测试代码,并进行必要的密钥替换等。我遇到的问题是在 $analytics->reports->batchGet( $body ); 运行 之后什么都不会 运行。更奇怪的是,它也没有在错误日志或控制台中给出任何类型的错误消息。

<?php

// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';


$analytics = initializeAnalytics();
print_r($analytics);
$response = getReport($analytics);
print_r("here");
print_r($response);
printResults($response);
print_r("here");


/**
 * Initializes an Analytics Reporting API V4 service object.
 *
 * @return An authorized Analytics Reporting API V4 service object.
 */
function initializeAnalytics()
{

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new Google_Service_AnalyticsReporting($client);

  return $analytics;
}


/**
 * Queries the Analytics Reporting API V4.
 *
 * @param service An authorized Analytics Reporting API V4 service object.
 * @return The Analytics Reporting API V4 response.
 */
function getReport($analytics) {

  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "view id is here";

  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("7daysAgo");
  $dateRange->setEndDate("today");

  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:sessions");
  $sessions->setAlias("sessions");

  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics(array($sessions));

  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();

  $body->setReportRequests( array( $request) );
  print_r($body);
  $temp = $analytics->reports->batchGet( $body );
  print_r("here3");

  return $temp;
}


/**
 * Parses and prints the Analytics Reporting API V4 response.
 *
 * @param An Analytics Reporting API V4 response.
 */
function printResults($reports) {
  for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
    $report = $reports[ $reportIndex ];
    $header = $report->getColumnHeader();
    $dimensionHeaders = $header->getDimensions();
    $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
    $rows = $report->getData()->getRows();

    for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
      $row = $rows[ $rowIndex ];
      $dimensions = $row->getDimensions();
      $metrics = $row->getMetrics();
      for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
        print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
      }

      for ($j = 0; $j < count($metrics); $j++) {
        $values = $metrics[$j]->getValues();
        for ($k = 0; $k < count($values); $k++) {
          $entry = $metricHeaders[$k];
          print($entry->getName() . ": " . $values[$k] . "\n");
        }
      }
    }
  }
}

我 运行 在 manjaro 安装 XAMPP 上安装它,如果有帮助的话。

理论上它应该 return 我认为是某种 JSON 字符串?但无论如何,代码不应该只是停止 运行ning.

我最好的猜测是 batchGet 函数内有问题挂起,但我不知道它是什么或如何解决它。

所以出于某种原因 XAMPP 没有显示任何错误消息,即使有错误消息也是如此。当我 运行 在命令行中仅使用 php 时,它显示了一条错误消息,就像我期望的那样(它与某些权限有关)。