Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (84 sloc) 3.55 KB
* Copyright 2018 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
// [START slides_quickstart]
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
* Returns an authorized API client.
* @return Google_Client the authorized client object
function getClient()
$client = new Google_Client();
$client->setApplicationName('Google Slides API PHP Quickstart');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
return $client;
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Slides($client);
// Prints the number of slides and elements in a sample presentation:
$presentationId = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc';
$presentation = $service->presentations->get($presentationId);
$slides = $presentation->getSlides();
printf("The presentation contains %s slides:\n", count($slides));
foreach ($slides as $i => $slide) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("- Slide #%s contains %s elements.\n", $i + 1,
// [END slides_quickstart]