Skip to content

Instantly share code, notes, and snippets.

@karlkranich
Created March 7, 2017 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlkranich/3193422429676f54fd373cc0e3224b1c to your computer and use it in GitHub Desktop.
Save karlkranich/3193422429676f54fd373cc0e3224b1c to your computer and use it in GitHub Desktop.
Google's PHP Quickstart adapted to use a service account
<?php
require_once __DIR__.'/vendor/autoload.php';
date_default_timezone_set('America/New_York');
define('SCOPES', implode(' ', array(
Google_Service_Sheets::SPREADSHEETS_READONLY)
));
putenv('GOOGLE_APPLICATION_CREDENTIALS=SheetsV4Test-b8b1388db3d6.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$service = new Google_Service_Sheets($client);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
$spreadsheetId = '1bqguOOiJMPm8HTR74TUx49WjGYIaiUQ7v_3PmNaoEGc';
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
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]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment