PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/v201109_1/BasicOperations/AddKeywords.php

http://google-api-adwords-php.googlecode.com/
PHP | 114 lines | 47 code | 17 blank | 50 comment | 3 complexity | cf3d13375a879daa2d65b3e52f7f5e60 MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. <?php
  2. /**
  3. * This example adds keywords to an ad group. To get ad groups run
  4. * GetAdGroups.php.
  5. *
  6. * Tags: AdGroupCriterionService.mutate
  7. * Restriction: adwords-only
  8. *
  9. * Copyright 2011, Google Inc. All Rights Reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * @package GoogleApiAdsAdWords
  24. * @subpackage v201109_1
  25. * @category WebServices
  26. * @copyright 2011, Google Inc. All Rights Reserved.
  27. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  28. * Version 2.0
  29. * @author Eric Koleda <eric.koleda@google.com>
  30. */
  31. error_reporting(E_STRICT | E_ALL);
  32. // Add the library to the include path. This is not neccessary if you've already
  33. // done so in your php.ini file.
  34. $path = dirname(__FILE__) . '/../../../src';
  35. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  36. require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.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 ID of the ad group to add the keywords to
  43. */
  44. function AddKeywordsExample(AdWordsUser $user, $adGroupId) {
  45. // Get the service, which loads the required classes.
  46. $adGroupCriterionService =
  47. $user->GetService('AdGroupCriterionService', 'v201109_1');
  48. $numKeywords = 5;
  49. $operations = array();
  50. for ($i = 0; $i < $numKeywords; $i++) {
  51. // Create keyword criterion.
  52. $keyword = new Keyword();
  53. $keyword->text = 'mars cruise ' . uniqid();
  54. $keyword->matchType = 'BROAD';
  55. // Create biddable ad group criterion.
  56. $adGroupCriterion = new BiddableAdGroupCriterion();
  57. $adGroupCriterion->adGroupId = $adGroupId;
  58. $adGroupCriterion->criterion = $keyword;
  59. // Set additional settings (optional).
  60. $adGroupCriterion->userStatus = 'PAUSED';
  61. $adGroupCriterion->destinationUrl = 'http://www.example.com/mars';
  62. // Set bids (optional).
  63. $bids = new ManualCPCAdGroupCriterionBids();
  64. $bids->maxCpc = new Bid(new Money(500000));
  65. $adGroupCriterion->bids = $bids;
  66. $adGroupCriteria[] = $adGroupCriterion;
  67. // Create operation.
  68. $operation = new AdGroupCriterionOperation();
  69. $operation->operand = $adGroupCriterion;
  70. $operation->operator = 'ADD';
  71. $operations[] = $operation;
  72. }
  73. // Make the mutate request.
  74. $result = $adGroupCriterionService->mutate($operations);
  75. // Display results.
  76. foreach ($result->value as $adGroupCriterion) {
  77. printf("Keyword with text '%s', match type '%s', and id '%s' was added.\n",
  78. $adGroupCriterion->criterion->text,
  79. $adGroupCriterion->criterion->matchType,
  80. $adGroupCriterion->criterion->id);
  81. }
  82. }
  83. // Don't run the example if the file is being included.
  84. if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  85. return;
  86. }
  87. try {
  88. // Get AdWordsUser from credentials in "../auth.ini"
  89. // relative to the AdWordsUser.php file's directory.
  90. $user = new AdWordsUser();
  91. // Log every SOAP XML request and response.
  92. $user->LogAll();
  93. // Run the example.
  94. AddKeywordsExample($user, $adGroupId);
  95. } catch (Exception $e) {
  96. printf("An error has occurred: %s\n", $e->getMessage());
  97. }