我运行了一个小型博客网站,并在帖子列表中显示GA页面浏览量。问题是获取过程需要大约20秒或更长时间。为了让它更快,我找到了Guzzle6应用程序接口,但我不知道如何将这些api组合在一起工作。我想要做的是运行GA api来同时获得帖子的页面浏览量。
或者如果你们知道其他更快获得每个帖子页面浏览量的方法,我将不胜感激!
谢谢。
作为参考,这里是我目前使用的GA api代码。(它工作得很好,但太慢了,无法获得多个帖子的页面浏览量。)
function views() {
require_once 'gapi.php';
$jsonPath = __DIR__ . '/THE-PATH';
$serviceClientId = 'THE-SC-ID';
$pID = 'P-ID';
$slug = get_the_slug_for_each_post();
$ga=new gapi($serviceClientId,$jsonPath);
date_default_timezone_set('Asia/Tokyo');
$dimensions = 'pagePath';
$metrics = 'Pageviews';
$sortMetric = null;
$filter = 'ga:pagePath=@/'.$slug;
$startDate = '2015-01-01';
$endDate = date('Y-m-d');
$startIndex = 1;
$maxResults = 10000;
$ga->requestReportData($pID, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);
foreach($ga->getResults()as$result) {
$views = $result->getPageviews();
}
return $views;
}发布于 2017-04-24 21:17:50
如果你查看composer file of the PHP API client,你会发现它已经使用了Guzzle。所以,没有必要采取行动。
"name": "google/apiclient",
"type": "library",
"description": "Client library for Google APIs",
"keywords": ["google"],
"homepage": "http://developers.google.com/api-client-library/php",
"license": "Apache-2.0",
"require": {
"php": ">=5.4",
"google/auth": "^0.11",
"google/apiclient-services": "^0.11",
"firebase/php-jwt": "~2.0|~3.0|~4.0",
"monolog/monolog": "^1.17",
"phpseclib/phpseclib": "~0.3.10|~2.0",
"guzzlehttp/guzzle": "~5.2|~6.0",
"guzzlehttp/psr7": "^1.2"
}为了解决您的问题,我建议您提前(大约每天一次)将数据下载到服务器上的数据库表中,然后从本地数据库中查询每页的数据。这不仅速度更快,而且还可以帮助您避免在站点增长时达到API的查询限制。
https://stackoverflow.com/questions/43588845
复制相似问题