Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements Google Forms API Sample #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions forms/quickstart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Google Forms API Quickstart

Complete the steps described in the [Google Forms API Quickstart](https://developers.google.com/forms/api/quickstart/php), and in about five minutes you'll have a simple PHP command-line application that makes requests to the Google Forms API.

## Set up

### Install Composer Globally

Before running this quickstart, be sure you have [Composer installed globally](https://getcomposer.org/doc/00-intro.md#globally).

```sh
composer install
```

### Download Developer Credentials

- Follow the steps in the quickstart instructions to download your developer
credentials and save them in a file called `credentails.json` in this
directory.

## Run

```sh
php quickstart.php
```
5 changes: 5 additions & 0 deletions forms/quickstart/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"google/apiclient": "^2.12.2"
}
}
87 changes: 87 additions & 0 deletions forms/quickstart/quickstart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright 2022 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START form_quickstart]
require __DIR__ . '/vendor/autoload.php';

/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Forms API PHP Quickstart');
$client->setScopes(Google_Service_Forms::FORMS_BODY_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');

// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('token.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} 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);

// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}

/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path)
{
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Forms($client);

// Prints the response link of the requested form:
// https://docs.google.com/forms/d/1UPwWDi4-oYVC7nPt_LISIc4jBnP5z4Z0qVUT4Rw5feg/edit
$formId = '1UPwWDi4-oYVC7nPt_LISIc4jBnP5z4Z0qVUT4Rw5feg';
$form = $service->forms->get($formId);

printf("The form response link is: %s\n", $form->getResponderUri());
// [END form_quickstart]