simple-query.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Make a simple API request using a key. In this
  21. example we're not making a request as a
  22. specific user, but simply indicating that the
  23. request comes from our application, and hence
  24. should use our quota, which is higher than the
  25. anonymous quota (which is limited per IP).
  26. ************************************************/
  27. require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
  28. /************************************************
  29. We create the client and set the simple API
  30. access key. If you comment out the call to
  31. setDeveloperKey, the request may still succeed
  32. using the anonymous quota.
  33. ************************************************/
  34. $client = new Google_Client();
  35. $client->setApplicationName("Client_Library_Examples");
  36. $apiKey = "<YOUR_API_KEY>"; // Change this line.
  37. // Warn if the API key isn't changed.
  38. if (strpos($apiKey, "<") !== false) {
  39. echo missingApiKeyWarning();
  40. exit;
  41. }
  42. $client->setDeveloperKey($apiKey);
  43. $service = new Google_Service_Books($client);
  44. /************************************************
  45. We make a call to our service, which will
  46. normally map to the structure of the API.
  47. In this case $service is Books API, the
  48. resource is volumes, and the method is
  49. listVolumes. We pass it a required parameters
  50. (the query), and an array of named optional
  51. parameters.
  52. ************************************************/
  53. $optParams = array('filter' => 'free-ebooks');
  54. $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
  55. /************************************************
  56. This call returns a list of volumes, so we
  57. can iterate over them as normal with any
  58. array.
  59. Some calls will return a single item which we
  60. can immediately use. The individual responses
  61. are typed as Google_Service_Books_Volume, but
  62. can be treated as an array.
  63. ***********************************************/
  64. echo "<h3>Results Of Call:</h3>";
  65. foreach ($results as $item) {
  66. echo $item['volumeInfo']['title'], "<br /> \n";
  67. }
  68. /************************************************
  69. This is an example of deferring a call.
  70. ***********************************************/
  71. $client->setDefer(true);
  72. $optParams = array('filter' => 'free-ebooks');
  73. $request = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
  74. $results = $client->execute($request);
  75. echo "<h3>Results Of Deferred Call:</h3>";
  76. foreach ($results as $item) {
  77. echo $item['volumeInfo']['title'], "<br /> \n";
  78. }
  79. echo pageFooter(__FILE__);