try {
require_once dirname(dirname(__FILE__)) . '/Google/init.php';
$user = new AdWordsUser();
$user->LogAll();
// Get the service, which loads the required classes.
$campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);
// Create selector.
$selector = new Selector();
$selector->fields = array('Id', 'Name', 'Labels');
// Labels filtering is performed by ID. You can use containsAny to select
// campaigns with any of the label IDs, containsAll to select campaigns with
// all of the label IDs, or containsNone to select campaigns with none of the
// label IDs.
$selector->predicates[] = new Predicate('Labels', 'CONTAINS_ANY',
array($labelId));
$selector->ordering[] = new OrderBy('Name', 'ASCENDING');
// Create paging controls.
$selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);
do {
// Make the get request.
$page = $campaignService->get($selector);
// Display results.
if (isset($page->entries)) {
foreach ($page->entries as $campaign) {
printf("Campaign with name '%s' and ID '%d' and labels '%s'" .
" was found.\n", $campaign->name, $campaign->id,
implode(', ',
array_map(function($label) {
return sprintf('%d/%s', $label->id, $label->name);
}, $campaign->labels)));
}
} else {
print "No campaigns were found.\n";
}
// Advance the paging index.
$selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
} while ($page->totalNumEntries > $selector->paging->startIndex);
} catch (Exception $e) {
printf("An error has occurred:".$e->getLine()." %s\n", $e->getMessage() );
// echo '<pre>';
// print_r($return_array);
// echo '</pre>';
// exit;
}