| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2013 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
include_once "templates/base.php"; |
| 18 |
echo pageHeader("Simple API Access"); |
| 19 |
|
| 20 |
/************************************************ |
| 21 |
Make a simple API request using a key. In this |
| 22 |
example we're not making a request as a |
| 23 |
specific user, but simply indicating that the |
| 24 |
request comes from our application, and hence |
| 25 |
should use our quota, which is higher than the |
| 26 |
anonymous quota (which is limited per IP). |
| 27 |
************************************************/ |
| 28 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 29 |
|
| 30 |
/************************************************ |
| 31 |
We create the client and set the simple API |
| 32 |
access key. If you comment out the call to |
| 33 |
setDeveloperKey, the request may still succeed |
| 34 |
using the anonymous quota. |
| 35 |
************************************************/ |
| 36 |
$client = new Google_Client(); |
| 37 |
$client->setApplicationName("Client_Library_Examples"); |
| 38 |
$apiKey = "<YOUR_API_KEY>"; // Change this line. |
| 39 |
// Warn if the API key isn't changed. |
| 40 |
if (strpos($apiKey, "<") !== false) { |
| 41 |
echo missingApiKeyWarning(); |
| 42 |
exit; |
| 43 |
} |
| 44 |
$client->setDeveloperKey($apiKey); |
| 45 |
|
| 46 |
$service = new Google_Service_Books($client); |
| 47 |
|
| 48 |
/************************************************ |
| 49 |
We make a call to our service, which will |
| 50 |
normally map to the structure of the API. |
| 51 |
In this case $service is Books API, the |
| 52 |
resource is volumes, and the method is |
| 53 |
listVolumes. We pass it a required parameters |
| 54 |
(the query), and an array of named optional |
| 55 |
parameters. |
| 56 |
************************************************/ |
| 57 |
$optParams = array('filter' => 'free-ebooks'); |
| 58 |
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); |
| 59 |
|
| 60 |
/************************************************ |
| 61 |
This call returns a list of volumes, so we |
| 62 |
can iterate over them as normal with any |
| 63 |
array. |
| 64 |
Some calls will return a single item which we |
| 65 |
can immediately use. The individual responses |
| 66 |
are typed as Google_Service_Books_Volume, but |
| 67 |
can be treated as an array. |
| 68 |
***********************************************/ |
| 69 |
echo "<h3>Results Of Call:</h3>"; |
| 70 |
foreach ($results as $item) { |
| 71 |
echo $item['volumeInfo']['title'], "<br /> \n"; |
| 72 |
} |
| 73 |
|
| 74 |
/************************************************ |
| 75 |
This is an example of deferring a call. |
| 76 |
***********************************************/ |
| 77 |
$client->setDefer(true); |
| 78 |
$optParams = array('filter' => 'free-ebooks'); |
| 79 |
$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams); |
| 80 |
$results = $client->execute($request); |
| 81 |
|
| 82 |
echo "<h3>Results Of Deferred Call:</h3>"; |
| 83 |
foreach ($results as $item) { |
| 84 |
echo $item['volumeInfo']['title'], "<br /> \n"; |
| 85 |
} |
| 86 |
|
| 87 |
echo pageFooter(__FILE__); |
| 88 |
|