[英]Error on integrating php and google sheets api
I am trying to integrate google sheets api with php so that I can capture html form data and append it to spreadsheet but I am facing a weird error.
下面是 php 片段:
$client = new \Google_Client();
$client->setApplicationName('WEBMONK_QUOTATION_REQUESTS');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig('../credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheets_id = '1S2LPDl5XmOEx4TQ3cR4yZ4SAALcxXRyxU5nFMU7RW0I';
$range = 'QUOTESHEET';
$sheet_rows = [
strval($datetime),
strval($name),
strval($email),
strval($url),
strval($extras)
];
$body = new Google_Service_Sheets_ValueRange(['values' => [$sheet_rows]]);
$params = ['valueInputOption' => 'RAW'];
$insert = ['insertDataOption' => 'INSERT_ROWS'];
$result = $service->spreadsheets_values->append(
$spreadsheets_id,
$range,
$body,
$params,
$insert
);
这是我得到的错误:
<br />
<b>Fatal error</b>: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in C:\xampp\htdocs\webric.org\api\vendor\google\apiclient\src\Google\Service\Resource.php:291
Stack trace:
#0 C:\xampp\htdocs\webric.org\api\vendor\google\apiclient\src\Google\Service\Resource.php(291): implode(Array, '&')
#1 C:\xampp\htdocs\webric.org\api\vendor\google\apiclient\src\Google\Service\Resource.php(190): Google_Service_Resource->createRequestUri('v4/spreadsheets...', Array)
#2 C:\xampp\htdocs\webric.org\api\vendor\google\apiclient-services\src\Google\Service\Sheets\Resource\SpreadsheetsValues.php(64): Google_Service_Resource->call('append', Array, 'Google_Service_...')
#3 C:\xampp\htdocs\webric.org\api\post\insert.php(68): Google_Service_Sheets_Resource_SpreadsheetsValues->append('1S2LPDl5XmOEx4T...', 'QUOTESHEET', Object(Google_Service_Sheets_ValueRange), Array, Array)
#4 {main}
thrown in <b>C:\xampp\htdocs\webric.org\api\vendor\google\apiclient\src\Google\Service\Resource.php</b> on line <b>291</b><br />
到目前为止,据我所知,由于参数类型错误,Google lib 中的 implode() function 出现故障。 但我找不到我上面的 php 代码有什么问题。 我已经完成了谷歌表格和 php 集成程序,如下所述: PHP with Google Sheets Quickstart
PHP 版本:8.0.0,谷歌 API 客户端:2.0
请告诉我哪里出错了。 提前致谢。
在切换到 PHP 8.0 后遇到了同样的问题(之前使用 PHP 7.2 工作正常)。
与以前的版本相比,PHP 8.0 中的 implode() function 基本上已经切换了两个 arguments。 我查看了 Google 库中 Resource.php 文件的最新版本,您应该看到第 303 行已经反映了这些更改。
我去了我的 Resource.php 文件并替换
$requestUrl .= '?' . implode($queryVars, '&');
和
$requestUrl .= '?' . implode('&', $queryVars);
它又开始工作了。 希望它有所帮助!
通过修改$body
来验证它
$body = new Google_Service_Sheets_ValueRange([
"values" => [[1, 2, 3]]
]);
如果此请求对您有用 - 记录[$sheet_rows]
以比较结构并查看问题所在。
PS:除了快速入门,还有PHP 的方法特定文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.