2

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.

Below is the php snippet:

$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
);

Here is the error I am getting:

<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, '&amp;')
#1 C:\xampp\htdocs\webric.org\api\vendor\google\apiclient\src\Google\Service\Resource.php(190): Google_Service_Resource-&gt;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-&gt;call('append', Array, 'Google_Service_...')
#3 C:\xampp\htdocs\webric.org\api\post\insert.php(68): Google_Service_Sheets_Resource_SpreadsheetsValues-&gt;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 />

So far, that I have understood, an implode() function in the Google lib is malfunctioning because of wrong argument type. But I couldn't find anything wrong with my above php code. I have gone through the google sheets and php integration procedures as mentioned here: PHP with Google Sheets Quickstart

PHP version: 8.0.0, Google API Client: 2.0

Please tell me where I am going wrong. Thanks in advance.

2
  • Its a code in the google api library. I have not gone through that code. Commented Dec 31, 2020 at 16:10
  • A parameter you are passing to one of the calls, should be an array but is not an array. Check the manual for all the calls you are making Commented Dec 31, 2020 at 16:12

2 Answers 2

3

Just had the same issue after switching to PHP 8.0 (previously working fine with PHP 7.2).

The implode() function in PHP 8.0 has basically the two arguments switched compared to previous versions. I checked out the lastest version of the Resource.php file in the Google library, and you should see that the line 303 reflects those changes already.

I went to my Resource.php file and replaced

$requestUrl .= '?' . implode($queryVars, '&');

with

$requestUrl .= '?' . implode('&', $queryVars);

And it's working again. Hope it helped!

Sign up to request clarification or add additional context in comments.

Comments

0

There must be a problem with your values

Verify it by modifying $body to

$body = new Google_Service_Sheets_ValueRange([
  "values" => [[1, 2, 3]]
]);

If this request works for you - log [$sheet_rows] to compare the structure and see what is wrong.

PS: Apart from the Quickstart, there is aslo method specific documentation for PHP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.