I am in search to find out what I am doing incorrectly in my attempt to get campaign stats. After a lot of difficulties correcting the references to file names, I have finally gotten rid of all error messages in my attempt to connect to the AdWords API. Now, though I am not getting any response whatsoever, I run my file and I get a blank screen. My code is below. Help on this matter, is greatly appreciated. I am still a novice when it comes to working with API's as this is my first time working with the AdWords API.
<?php
// Initialize the AdWords User
require_once 'Api/Ads/AdWords/Lib/AdWordsUser.php';
function GetCampaignStatsExample(AdWordsUser $user) {
// Get the service, which loads the required classes.
// Create selector.
$selector = new Selector();
$selector->fields =
array('Id', 'Name', 'Impressions', 'Clicks', 'Cost', 'Ctr');
$selector->predicates[] =
new Predicate('Impressions', 'GREATER_THAN', array(0));
// Set date range to request stats for.
$dateRange = new DateRange();
$dateRange->min = date('Ymd', strtotime('-1 week'));
$dateRange->max = date('Ymd', strtotime('-1 day'));
$selector->dateRange = $dateRange;
// 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 '%s' had the following stats "
. "during the last week:\n", $campaign->name, $campaign->id);
printf(" Impressions: %d\n", $campaign->campaignStats->impressions);
printf(" Clicks: %d\n", $campaign->campaignStats->clicks);
printf(" Cost: $%.2f\n", $campaign->campaignStats->cost->microAmount
/ AdWordsConstants::MICROS_PER_DOLLAR);
printf(" CTR: %.2f%%\n", $campaign->campaignStats->ctr * 100);
}
} else {
print "No matching campaigns were found.\n";
}
// Advance the paging index.
$selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
} while ($page->totalNumEntries > $selector->paging->startIndex);
}
// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
return;
}
try {
// Get AdWordsUser from credentials in "../auth.ini"
// relative to the AdWordsUser.php file's directory.
$user = new AdWordsUser();
// Log every SOAP XML request and response.
$user->LogAll();
// Run the example.
GetCampaignStatsExample($user);
} catch (OAuth2Exception $e) {
ExampleUtils::CheckForOAuth2Errors($e);
} catch (ValidationException $e) {
ExampleUtils::CheckForOAuth2Errors($e);
} catch (Exception $e) {
printf("An error has occurred: %s\n", $e->getMessage());
}
?>