| 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("Batching Queries"); |
| 19 |
|
| 20 |
/************************************************ |
| 21 |
We're going to use the simple access to the |
| 22 |
books API again as an example, but this time we |
| 23 |
will batch up two queries into a single call. |
| 24 |
************************************************/ |
| 25 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 26 |
|
| 27 |
/************************************************ |
| 28 |
We create the client and set the simple API |
| 29 |
access key. If you comment out the call to |
| 30 |
setDeveloperKey, the request may still succeed |
| 31 |
using the anonymous quota. |
| 32 |
************************************************/ |
| 33 |
$client = new Google_Client(); |
| 34 |
$client->setApplicationName("Client_Library_Examples"); |
| 35 |
$apiKey = "<YOUR_API_KEY>"; // Change to your API key. |
| 36 |
// Warn if the API key isn't changed! |
| 37 |
if (strpos($apiKey, "<") !== false) { |
| 38 |
echo missingApiKeyWarning(); |
| 39 |
exit; |
| 40 |
} else { |
| 41 |
$client->setDeveloperKey($apiKey); |
| 42 |
|
| 43 |
$service = new Google_Service_Books($client); |
| 44 |
|
| 45 |
/************************************************ |
| 46 |
To actually make the batch call we need to |
| 47 |
enable batching on the client - this will apply |
| 48 |
globally until we set it to false. This causes |
| 49 |
call to the service methods to return the query |
| 50 |
rather than immediately executing. |
| 51 |
************************************************/ |
| 52 |
$client->setUseBatch(true); |
| 53 |
|
| 54 |
/************************************************ |
| 55 |
We then create a batch, and add each query we |
| 56 |
want to execute with keys of our choice - these |
| 57 |
keys will be reflected in the returned array. |
| 58 |
************************************************/ |
| 59 |
$batch = new Google_Http_Batch($client); |
| 60 |
$optParams = array('filter' => 'free-ebooks'); |
| 61 |
$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams); |
| 62 |
$batch->add($req1, "thoreau"); |
| 63 |
$req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams); |
| 64 |
$batch->add($req2, "shaw"); |
| 65 |
|
| 66 |
/************************************************ |
| 67 |
Executing the batch will send all requests off |
| 68 |
at once. |
| 69 |
************************************************/ |
| 70 |
$results = $batch->execute(); |
| 71 |
|
| 72 |
echo "<h3>Results Of Call 1:</h3>"; |
| 73 |
foreach ($results['response-thoreau'] as $item) { |
| 74 |
echo $item['volumeInfo']['title'], "<br /> \n"; |
| 75 |
} |
| 76 |
echo "<h3>Results Of Call 2:</h3>"; |
| 77 |
foreach ($results['response-shaw'] as $item) { |
| 78 |
echo $item['volumeInfo']['title'], "<br /> \n"; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
echo pageFooter(__FILE__); |
| 83 |
|