google adwords clientcustomer id使用http请求?

时间:2016-01-09 10:05:33

标签: google-adwords

我通过传递必需参数获得了使用http请求的adword报告。但是在该参数中我已经硬编码了我的 customerclientid 。现在我想要检索我的所有 customerclientid 谷歌adwords帐户使用简单的http请求谷歌adword服务器。请帮帮我朋友

1 个答案:

答案 0 :(得分:0)

You can use following code to retrieve all accounts clientids.

Adwords Lib Ref : https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201506/AccountManagement/GetAccountHierarchy.php

function GetAllClients(AdWordsUser $user) {

  $managedCustomerService = $user->GetService('ManagedCustomerService', ADWORDS_VERSION);

  $selector = new Selector();
  $selector->fields = array('CustomerId',  'Name');
  $graph = $managedCustomerService->get($selector);

  if (isset($graph->entries)) {
    $childLinks = array();
    $parentLinks = array();
    if (isset($graph->links)) {
      foreach ($graph->links as $link) {
        $childLinks[$link->managerCustomerId][] = $link;
        $parentLinks[$link->clientCustomerId][] = $link;
      }
    }

    $accounts = array();
    $rootAccount = null;
    foreach ($graph->entries as $account) {
      $accounts[$account->customerId] = $account;
      if (!array_key_exists($account->customerId, $parentLinks)) {
        $rootAccount = $account;
      }
    }
    if (!isset($rootAccount)) {
      $rootAccount = new Account();
      $rootAccount->customerId = 0;
    }
    print "(Customer Id, Account Name)\n";
    DisplayAccountTree($rootAccount, null, $accounts, $childLinks, 0);
  } else {
    print "No serviced accounts were found.\n";
  }
}

function DisplayAccountTree($account, $link, $accounts, $links, $depth) {
  print str_repeat('-', $depth * 2);
  printf("%s, %s\n", $account->customerId, $account->name);
  if (array_key_exists($account->customerId, $links)) {
    foreach ($links[$account->customerId] as $childLink) {
      $childAccount = $accounts[$childLink->clientCustomerId];
      DisplayAccountTree($childAccount, $childLink, $accounts, $links,
          $depth +1);
    }
  }
}
try {
  $user = new AdWordsUser();
  GetAllClients($user);
} catch (Exception $e) {
  printf("An error has occurred: %s\n", $e->getMessage());
}