PHP调用Google Analytics batchRunReports方法报错,求助解决方案
问题场景
尝试在PHP中调用Google Analytics Data API的batchRunReports方法,编写代码后触发致命错误。在官方文档的测试工具中能正常运行查询,但PHP代码无法实现。
原代码
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\DateRange; use Google\Analytics\Data\V1beta\Dimension; use Google\Analytics\Data\V1beta\Metric; use Google\Analytics\Data\V1beta\MetricAggregation; $property = "properties/XXXXXXXXX"; $client = new BetaAnalyticsDataClient(); $client->batchRunReports([ "requests" => [ [ "property" => $property, "dataRanges" => [ new DateRange(["start_date" => "7daysAgo"], ["end_date" => "today"]), ], "dimensions" => [ new Dimension(["name" => "eventName"]), ], "metrics" => [ new Metric(["name" => "eventCount"]), ] ], [ "property" => $property, "dataRanges" => [ new DateRange(["start_date" => "7daysAgo"], ["end_date" => "today"]), ], "dimensions" => [ new Dimension(["name" => "deviceCategory"]), ], "metrics" => [ new Metric(["name" => "activeUsers"]), ] ], ] ]);
错误信息
Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\RunReportRequest. in F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php:198 Stack trace:
#0 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\RepeatedField.php(187): Google\Protobuf\Internal\GPBUtil::checkMessage(Array, 'Google\Analytic...')
#1 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php(210): Google\Protobuf\Internal\RepeatedField->offsetSet(NULL, Array)
#2 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\analytics-data\src\V1beta\BatchRunReportsRequest.php(126): Google\Protobuf\Internal\GPBUtil::checkRepeatedField(Array, 11, 'Google\Analytic...')
#3 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\analytics-data\src\V1beta\Gapic\BetaAnalyticsDataGapicClient.php(421): Google\Analytics\Data\V1beta\BatchRunReportsRequest->setRequests(Array)
#4 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\test.php(46): Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient->batchRunReports(Array)
#5 {main} thrown in F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php on line 198
错误原因
- 请求格式错误:
batchRunReports的requests参数要求传入RunReportRequest对象实例,而非关联数组。原代码直接用数组定义请求,不符合API要求。 - DateRange构造错误:
DateRange的构造函数只需传入包含start_date和end_date的单个数组,原代码错误地传入了两个分离的数组。 - 字段名不匹配:PHP客户端要求日期范围字段名为
date_ranges,原代码误用了dataRanges。
修正后的代码
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\DateRange; use Google\Analytics\Data\V1beta\Dimension; use Google\Analytics\Data\V1beta\Metric; use Google\Analytics\Data\V1beta\RunReportRequest; // 新增引入类 $property = "properties/XXXXXXXXX"; $client = new BetaAnalyticsDataClient(); // 构造第一个报表请求 $request1 = new RunReportRequest([ "property" => $property, "date_ranges" => [ new DateRange(["start_date" => "7daysAgo", "end_date" => "today"]), ], "dimensions" => [ new Dimension(["name" => "eventName"]), ], "metrics" => [ new Metric(["name" => "eventCount"]), ] ]); // 构造第二个报表请求 $request2 = new RunReportRequest([ "property" => $property, "date_ranges" => [ new DateRange(["start_date" => "7daysAgo", "end_date" => "today"]), ], "dimensions" => [ new Dimension(["name" => "deviceCategory"]), ], "metrics" => [ new Metric(["name" => "activeUsers"]), ] ]); // 执行批量请求 $response = $client->batchRunReports([ "requests" => [$request1, $request2] ]); // 输出结果示例 foreach ($response->getReports() as $index => $report) { echo "报表 " . ($index + 1) . " 结果:\n"; // 可根据需求遍历维度、指标数据 }
关键修正点
- 新增引入
RunReportRequest类,将每个报表请求封装为该类的实例。 - 修正
DateRange的构造方式,把start_date和end_date合并到同一个数组参数中。 - 将
dataRanges改为PHP客户端要求的date_ranges字段名。
内容的提问来源于stack exchange,提问作者Berkay Kaya

