The Google documentation "PHP Quick Start" for the Sheet API is deprecated at https://developers.google.com/sheets/api/quickstart/php .
To make their demo work with PHP 7. 2+ I had to change a bit, and itβs not entirely clear what is happening. Below is an updated version of their quick launch with comments, which can be useful to anyone who has difficulty working with the Google Sheets API and PHP.
<?php /** * Updated Google Sheets Quickstart * * https://developers.google.com/sheets/api/quickstart/php */ require __DIR__ . '/vendor/autoload.php'; /** * Appends one slash at the end, and removes any extra slashes * https://stackoverflow.com/a/9339669/812973 * * @return string $path with the slash appended */ function addTrailingSlash ($path) { return rtrim ($path, '/') . '/'; } /** * Returns an authorized API client. * @return Google_Client the authorized client object */ function getClient() { // Change this to a secure location where you'll save your config *.json files // Make sure it isn't publicly available on the web $configPath = addTrailingSlash (getcwd()); // This get generated by the script, so don't create it $credentialsPath = $configPath . 'credentials.json'; $client = new Google_Client(); // Matches the "Application Name" you enter during the "Step 1" wizard $client->setApplicationName( 'API App Name' ); $client->setScopes( Google_Service_Sheets::SPREADSHEETS_READONLY ); // You need to go through "Step 1" steps to generate this file: https://developers.google.com/sheets/api/quickstart/php $client->setAuthConfig( $configPath . 'client_secret.json' ); $client->setAccessType( 'offline' ); // This must match the "callback URL" that you enter under "OAuth 2.0 client ID" in the Google APIs console at https://console.developers.google.com/apis/credentials $client->setRedirectUri( 'https://' . $_SERVER['HTTP_HOST'] . '/' . basename( __FILE__, '.php' ) ); // We have a stored credentials file, try using the data from there first if ( file_exists( $credentialsPath ) ) { $accessToken = json_decode( file_get_contents( $credentialsPath ), true ); } // No stored credentials found, we'll need to request them with OAuth else { // Request authorization from the user $authUrl = $client->createAuthUrl(); if ( ! isset( $_GET['code'] ) ) { header( "Location: $authUrl", true, 302 ); exit; } // The authorization code is sent to the callback URL as a GET parameter. // We use this "authorization code" to generate an "access token". The // "access token" is what effectively used as a private API key. $authCode = $_GET['code']; $accessToken = $client->fetchAccessTokenWithAuthCode( $authCode ); // Create credentials.json if it does not already exist (first run) if ( ! file_exists( dirname( $credentialsPath ) ) ) { mkdir( dirname( $credentialsPath ), 0700, true ); } // Save the $accessToken object to the credentials.json file for re-use file_put_contents( $credentialsPath, json_encode( $accessToken ) ); } // Provide client with API access token $client->setAccessToken( $accessToken ); // If the $accessToken is expired then we'll need to refresh it if ( $client->isAccessTokenExpired() ) { $client->fetchAccessTokenWithRefreshToken( $client->getRefreshToken() ); file_put_contents( $credentialsPath, json_encode( $client->getAccessToken() ) ); } return $client; } // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Sheets( $client ); // Get values from a spreadheet and print // https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get $spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'; $range = 'Class Data!A2:E'; $response = $service->spreadsheets_values->get($spreadsheetId, $range); $values = $response->getValues(); if (empty($values)) { print "No data found.\n"; } else { print "Name, Major:\n"; foreach ($values as $row) { // Print columns A and E, which correspond to indices 0 and 4. printf("%s, %s\n", $row[0], $row[4]); } }
source share