Google PHP API 返回无效参数错误
Posted
技术标签:
【中文标题】Google PHP API 返回无效参数错误【英文标题】:Google PHP API returning invalid argument error 【发布时间】:2022-01-07 16:41:17 【问题描述】:我正在尝试通过 Google php 库获取位置信息。 我正在为 -> 检索给定位置列表的基本见解做示例。 来自这个页面:https://support.google.com/business/thread/1774669/php-code-example-insights-with-mybusiness-php-v3-2?hl=en
require_once $_SERVER['DOCUMENT_ROOT']."/google/auth_google.php";
require_once $_SERVER['DOCUMENT_ROOT']."/MyBusiness.php";
$mybusinessService = new Google_Service_Mybusiness($client);
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
$locationStatistics = [];
//
foreach ($accountsList as $accKey => $account)
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
//
$firstDay = date("Y-m-d\TH:i:s.000\Z", strtotime(date('Y-m-1')));
$lastDay = date("Y-m-d\TH:i:s.000\Z", strtotime("today"));
// // Final Goal of my Code
if (empty($locationsList)===false)
foreach ($locationsList as $locKey => $location)
$reviewLink = $location['metadata']['newReviewUrl'];
$locationNames = $location->locationName;
$reportLocationInsightsRequest = new Google_Service_MyBusiness_ReportLocationInsightsRequest($client);
$basicRequest = new Google_Service_MyBusiness_BasicMetricsRequest($client);
$metricRequests = new Google_Service_MyBusiness_MetricRequest($client);
$metricRequests->setMetric("ALL");
$basicRequest->setMetricRequests($metricRequests);
$timeRange = new Google_Service_MyBusiness_TimeRange($client);
$timeRange->setStartTime($firstDay);
$timeRange->setEndTime($lastDay);
$basicRequest->setTimeRange($timeRange);
$reportLocationInsightsRequest->setBasicRequest($basicRequest);
$reportLocationInsightsRequest->setLocationNames(array($location->name));
$reportLocationInsightsResponse = $locations->reportInsights($account->name, $reportLocationInsightsRequest);
$locationMetrics = $reportLocationInsightsResponse->getLocationMetrics();
$locationMetricsArray = array();
$locationMetricsArray['locationMetrics']['locationName'] = array($locationMetrics[0]->getLocationName());
$locationMetricsArray['locationMetrics']['timeZone'] = array($locationMetrics[0]->getTimeZone());
$metricValuesArray = array();
foreach ($locationMetrics[0]->getMetricValues() as $value)
$metricValuesItem = array();
$metricValuesItem['metric'] = $value['metric'];
$metricValuesItem['totalValue']['timeDimension']['timeRange']['endTime'] = $value['totalValue']['timeDimension']['timeRange']['endTime'];
$metricValuesItem['totalValue']['timeDimension']['timeRange']['startTime'] = $value['totalValue']['timeDimension']['timeRange']['startTime'];
$metricValuesItem['totalValue']['metricOption'] = $value['totalValue']['metricOption'];
$metricValuesItem['totalValue']['value'] = $value['totalValue']['value'];
$metricValuesArray[] = $metricValuesItem;
$locationMetricsArray['locationMetrics']['metricValues'] = $metricValuesArray ;
$locData =array();
$locID = str_replace('https://search.google.com/local/writereview?placeid=', '', $reviewLink);
$locData['name'] = $locationNames;
$locData['reviewLink'] = $reviewLink;
$locData['locationID'] = $locID;
但我收到以下错误:
PHP Fatal error: Uncaught Google\Service\Exception:
"error":
"code": 400,
"message": "Request contains an invalid argument.",
"errors": [
"message": "Request contains an invalid argument.",
"domain": "global",
"reason": "badRequest"
],
"status": "INVALID_ARGUMENT"
【问题讨论】:
【参考方案1】:代码没有问题。问题发生在
$firstDay = date("Y-m-d\TH:i:s.000\Z", strtotime(date('Y-m-1')));
$lastDay = date("Y-m-d\TH:i:s.000\Z", strtotime("today"));
如果 $firstDay 和 $lastDay 相等,API 会返回错误。
【讨论】:
以上是关于Google PHP API 返回无效参数错误的主要内容,如果未能解决你的问题,请参考以下文章
请求AppDataFolder时,Google云端硬盘API会返回错误“无效值”和“无效查询”
facebook 发送 API 错误代码:100 API 错误描述:无效参数错误消息:“链接”无效