google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Getting request back instead of appending rows into a google sheet?

Open rajklakhani opened this issue 8 months ago • 1 comments

function insert_data_to_google_sheet($data): void {
    $client = getGoogleClient();
    $service = new \Google_Service_Sheets($client);
    $spreadsheetId = '1BgVqitbiMSP2sNQNy1hk0PCoItxacbyeZvjH8Zc2qu0';
     $range = 'Sheet1';
    $values = [
        $data
    ];

  $valueRange = new \Google_Service_Sheets_ValueRange();
  $valueRange->setValues($values);
  // I have tried the following statement as well
  // $body = new \Google_Service_Sheets_ValueRange([
  //     "values" => $values
  // ]); 


  $params = [
      'valueInputOption' => 'RAW'
  ];

  // appending record to sheet
  $result = $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $params);

  echo '<pre>';
  print_r($result);
  echo '</pre>';
  // return $result;
}

$result is showing the following output instead of inserting the data:

GuzzleHttp\Psr7\Request Object
(
    [method:GuzzleHttp\Psr7\Request:private] => POST
    [requestTarget:GuzzleHttp\Psr7\Request:private] => 
    [uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object
        (
            [scheme:GuzzleHttp\Psr7\Uri:private] => https
            [userInfo:GuzzleHttp\Psr7\Uri:private] => 
            [host:GuzzleHttp\Psr7\Uri:private] => sheets.googleapis.com
            [port:GuzzleHttp\Psr7\Uri:private] => 
            [path:GuzzleHttp\Psr7\Uri:private] => /v4/spreadsheets/1BgVqitbiMSP2sNQNy1hk0PCoItxacbyeZvjH8Zc2qu0/values/Sheet1:append
            [query:GuzzleHttp\Psr7\Uri:private] => valueInputOption=RAW
            [fragment:GuzzleHttp\Psr7\Uri:private] => 
        )

    [headers:GuzzleHttp\Psr7\Request:private] => Array
        (
            [host] => Array
                (
                    [0] => sheets.googleapis.com
                )

            [content-type] => Array
                (
                    [0] => application/json
                )

            [x-php-expected-class] => Array
                (
                    [0] => Google\Service\Sheets\AppendValuesResponse
                )

        )

    [headerLines:GuzzleHttp\Psr7\Request:private] => Array
        (
            [Host] => Array
                (
                    [0] => sheets.googleapis.com
                )

            [content-type] => Array
                (
                    [0] => application/json
                )

            [X-Php-Expected-Class] => Array
                (
                    [0] => Google\Service\Sheets\AppendValuesResponse
                )
        )

    [protocol:GuzzleHttp\Psr7\Request:private] => 1.1
    [stream:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #148
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )
        )
)

I need urgent help, please connect with someone who has already faced an issue like this.

Thanks

image

rajklakhani avatar Jun 02 '24 15:06 rajklakhani