batch.php 3.0 KB

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