PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/v201109_1/ErrorHandling/HandlePolicyViolationError.php

http://google-api-adwords-php.googlecode.com/
PHP | 151 lines | 80 code | 17 blank | 54 comment | 13 complexity | 06f89ebffa31bfcd2d25027894332a47 MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. <?php
  2. /**
  3. * This example demonstrates how to handle policy violation errors when creating
  4. * text ads.
  5. *
  6. * Tags: AdGroupAdService.mutate
  7. *
  8. * Copyright 2011, Google Inc. All Rights Reserved.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * @package GoogleApiAdsAdWords
  23. * @subpackage v201109_1
  24. * @category WebServices
  25. * @copyright 2011, Google Inc. All Rights Reserved.
  26. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  27. * Version 2.0
  28. * @author Eric Koleda <eric.koleda@google.com>
  29. */
  30. error_reporting(E_STRICT | E_ALL);
  31. // Add the library to the include path. This is not neccessary if you've already
  32. // done so in your php.ini file.
  33. $path = dirname(__FILE__) . '/../../../src';
  34. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  35. require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
  36. require_once 'Google/Api/Ads/Common/Util/ErrorUtils.php';
  37. // Enter parameters required by the code example.
  38. $adGroupId = 'INSERT_AD_GROUP_ID_HERE';
  39. /**
  40. * Runs the example.
  41. * @param AdWordsUser $user the user to run the example with
  42. * @param string $adGroupId the if the ad group to add the text ads to
  43. */
  44. function HandlePolicyViolationErrorExample(AdWordsUser $user, $adGroupId) {
  45. // Get the service, which loads the required classes.
  46. $adGroupAdService = $user->GetService('AdGroupAdService', 'v201109_1');
  47. // Get validateOnly version of the AdGroupAdService.
  48. $adGroupAdValidationService =
  49. $user->GetService('AdGroupAdService', 'v201109_1', NULL, NULL, TRUE);
  50. // Create text ad that violates an exemptable policy. This ad will only
  51. // trigger an error in the production environment.
  52. $textAd = new TextAd();
  53. $textAd->headline = 'Mars Cruise !!!';
  54. $textAd->description1 = 'Visit the Red Planet in style.';
  55. $textAd->description2 = 'Low-gravity fun for everyone!';
  56. $textAd->displayUrl = 'www.example.com';
  57. $textAd->url = 'http://www.example.com/';
  58. // Create ad group ad.
  59. $adGroupAd = new AdGroupAd();
  60. $adGroupAd->adGroupId = $adGroupId;
  61. $adGroupAd->ad = $textAd;
  62. // Create operation.
  63. $operation = new AdGroupAdOperation();
  64. $operation->operand = $adGroupAd;
  65. $operation->operator = 'ADD';
  66. $operations = array($operation);
  67. try {
  68. // Make the mutate request.
  69. $result = $adGroupAdValidationService->mutate($operations);
  70. } catch (SoapFault $fault) {
  71. $errors = ErrorUtils::GetApiErrors($fault);
  72. if (sizeof($errors) == 0) {
  73. // Not an API error, so throw fault.
  74. throw $fault;
  75. }
  76. $operationIndicesToRemove = array();
  77. foreach ($errors as $error) {
  78. if ($error->ApiErrorType == 'PolicyViolationError') {
  79. $operationIndex = ErrorUtils::GetSourceOperationIndex($error);
  80. $operation = $operations[$operationIndex];
  81. printf("Ad with headline '%s' violated %s policy '%s'.\n",
  82. $operation->operand->ad->headline,
  83. $error->isExemptable ? 'exemptable' : 'non-exemptable',
  84. $error->externalPolicyName);
  85. if ($error->isExemptable) {
  86. // Add exemption request to the operation.
  87. printf("Adding exemption request for policy name '%s' on text "
  88. ."'%s'.\n", $error->key->policyName, $error->key->violatingText);
  89. $operation->exemptionRequests[] = new ExemptionRequest($error->key);
  90. } else {
  91. // Remove non-exemptable operation.
  92. print "Removing the operation from the request.\n";
  93. $operationIndicesToRemove[] = $operationIndex;
  94. }
  95. } else {
  96. // Non-policy error returned, throw fault.
  97. throw $fault;
  98. }
  99. }
  100. $operationIndicesToRemove = array_unique($operationIndicesToRemove);
  101. rsort($operationIndicesToRemove, SORT_NUMERIC);
  102. if (sizeof($operationIndicesToRemove) > 0) {
  103. foreach ($operationIndicesToRemove as $operationIndex) {
  104. unset($operations[$operationIndex]);
  105. }
  106. }
  107. }
  108. if (sizeof($operations) > 0) {
  109. // Retry the mutate request.
  110. $result = $adGroupAdService->mutate($operations);
  111. // Display results.
  112. foreach ($result->value as $adGroupAd) {
  113. printf("Text ad with headline '%s' and id '%s' was added.\n",
  114. $adGroupAd->ad->headline, $adGroupAd->ad->id);
  115. }
  116. } else {
  117. print "All the operations were invalid with non-exemptable errors.\n";
  118. }
  119. }
  120. // Don't run the example if the file is being included.
  121. if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  122. return;
  123. }
  124. try {
  125. // Get AdWordsUser from credentials in "../auth.ini"
  126. // relative to the AdWordsUser.php file's directory.
  127. $user = new AdWordsUser();
  128. // Log every SOAP XML request and response.
  129. $user->LogAll();
  130. // Run the example.
  131. HandlePolicyViolationErrorExample($user, $adGroupId);
  132. } catch (Exception $e) {
  133. printf("An error has occurred: %s\n", $e->getMessage());
  134. }