/examples/adexchangebuyer/index.php

https://bitbucket.org/rdcli/php-google-api-client · PHP · 98 lines · 50 code · 10 blank · 38 comment · 7 complexity · d651be1d9d4f411dc8e5d8cab5f44c0b MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2012 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. /**
  18. * Implements the examples execution flow.
  19. * Load this file with no parameters to get the list of available examples.
  20. *
  21. * @author David Torres <david.t@google.com>
  22. */
  23. require_once "../../src/apiClient.php";
  24. require_once "../../src/contrib/apiAdexchangebuyerService.php";
  25. require_once "htmlHelper.php";
  26. session_start();
  27. $client = new apiClient();
  28. $client->setApplicationName('DoubleClick Ad Exchange Buyer API PHP Samples');
  29. // Visit https://code.google.com/apis/console?api=adexchangebuyer to generate
  30. // your client id, client secret, and to register your redirect uri.
  31. $client->setScopes(array('https://www.googleapis.com/auth/adexchange.buyer'));
  32. // Visit https://code.google.com/apis/console?api=adexchangebuyer to generate
  33. // your oauth2_client_id, oauth2_client_secret, and to register your
  34. // oauth2_redirect_uri.
  35. // $client->setClientId('insert_your_oauth2_client_id');
  36. // $client->setClientSecret('insert_your_oauth2_client_secret');
  37. // $client->setRedirectUri('insert_your_oauth2_redirect_uri');
  38. // $client->setDeveloperKey('insert_your_simple_api_key');
  39. $service = new apiAdexchangebuyerService($client);
  40. if (isset($_GET['code'])) {
  41. $client->authenticate();
  42. $_SESSION['token'] = $client->getAccessToken();
  43. $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  44. header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  45. }
  46. if (isset($_SESSION['token'])) {
  47. $client->setAccessToken($_SESSION['token']);
  48. }
  49. if ($client->getAccessToken()) {
  50. // Build the list of supported actions.
  51. $actions = getSupportedActions();
  52. // If the action is set dispatch the action if supported
  53. if (isset($_GET["action"])) {
  54. $action = $_GET["action"];
  55. if (!in_array($action, $actions)) {
  56. die('Unsupported action:' . $action . "\n");
  57. }
  58. // Render the required action.
  59. require_once 'examples/' . $action . '.php';
  60. $class = ucfirst($action);
  61. $example = new $class($service);
  62. printHtmlHeader($example->getName());
  63. try {
  64. $example->execute();
  65. } catch (apiException $ex) {
  66. printf('An error as occurred while calling the example:<br/>');
  67. printf($ex->getMessage());
  68. }
  69. printSampleHtmlFooter();
  70. } else {
  71. // Show the list of links to supported actions.
  72. printHtmlHeader('Ad Exchange Buyer API PHP usage examples.');
  73. printExamplesIndex($actions);
  74. printHtmlFooter();
  75. }
  76. // The access token may have been updated.
  77. $_SESSION['token'] = $client->getAccessToken();
  78. } else {
  79. $authUrl = $client->createAuthUrl();
  80. print "<a class='login' href='$authUrl'>Connect Me!</a>";
  81. }
  82. /**
  83. * Builds an array containing the supported actions.
  84. */
  85. function getSupportedActions() {
  86. return array('GetAllAccounts', 'GetCreative', 'GetDirectDeals',
  87. 'SubmitCreative', 'UpdateAccount');
  88. }