因此,我们有一个有效的API,可以在特定日期范围内获取报告。最近,我们尝试获取行数达到40,000个结果的报告。我们从API得到了响应,并且能够提取前1000行。
现在问题在于分页。查询设置为每批次检索1,000行,因此总共40批次(基于40,000行)。根据{{3}},从api响应中检索nextPageToken并将其设置为下一个批处理请求。大多数情况下,我们得到的报告不完整,因为从响应返回的nextPageToken为空。因此,我们无法继续获取下一批。我们尝试手动填充analytics api pagination,因为它是从零开始的行索引,但是api调用失败。
您知道为什么响应中的nextPageToken随机失败吗?
迭代代码:
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$data = $service->reports->batchGet( $body );
$cnt = 0;
while ($data->reports[0]->nextPageToken > 0 && $cnt < 10) {
\Log::info($data->reports[0]->nextPageToken); // Token log
$body->reportRequests[0]->setPageToken($data->reports[0]->nextPageToken);
$data = $service->reports->batchGet( $body );
showData($data->reports[0]->nextPageToken);
$cnt++;
}
Log results:
Attempt 1
[2018-08-28 03:43:57] local.INFO: 1000
[2018-08-28 03:43:59] local.INFO: 2000
[2018-08-28 03:44:04] local.INFO: (failed/ empty next page token)
Attempt 2
[2018-08-28 03:45:01] local.INFO: 1000
[2018-08-28 03:45:26] local.INFO: 2000
[2018-08-28 03:45:42] local.INFO: (failed/ empty next page token)
Attempt 3
[2018-08-28 03:48:47] local.INFO: 1000
[2018-08-28 03:49:01] local.INFO: 2000
[2018-08-28 03:49:15] local.INFO: 3000
[2018-08-28 03:49:31] local.INFO: 4000
[2018-08-28 03:49:46] local.INFO: 5000
[2018-08-28 03:50:02] local.INFO: (failed/ empty next page token)
Attempt 4
[2018-08-28 03:54:10] local.INFO: 1000
[2018-08-28 03:54:12] local.INFO: 2000
[2018-08-28 03:54:13] local.INFO: 3000
[2018-08-28 03:54:14] local.INFO: 4000
[2018-08-28 03:54:15] local.INFO: 5000
[2018-08-28 03:54:16] local.INFO: 6000
[2018-08-28 03:54:31] local.INFO: (failed/ empty next page token)
答案 0 :(得分:0)
您需要设置每个页面令牌,例如nextPageToken
do {
//your processing code
$request->setPageToken($response[0]->getNextPageToken());//set next page token
} while ($response[0]->getNextPageToken() != '');