Created
March 7, 2017 01:27
-
-
Save karlkranich/3193422429676f54fd373cc0e3224b1c to your computer and use it in GitHub Desktop.
Google's PHP Quickstart adapted to use a service account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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