[英]AdWords API PHP Issues
我在找出与PHP中的Adwords API一起使用的代码有什么问题时遇到了一些实际问题。 我正在尝试检索简单的广告系列统计信息,以输入本地数据库。 但是,我无法做到这一点。 我使用的是最初下载的示例,但是返回的是一个完全空白的屏幕,根本没有任何有用的错误消息。 下面是我的代码,我希望这里有人遇到过这个问题,可以帮助我解决这个问题。
require_once 'Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'Api/Ads/AdWords/Util/ReportUtils.php';
function GetCampaignStatsExample(AdWordsUser $user) {
// Get the service, which loads the required classes.
$campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);
// 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) {
print_r("Campaign with name '%s' and id '%s' had the following stats " . "during the last week:\n", $campaign->name, $campaign->id);
print_r(" Impressions: %d\n", $campaign->campaignStats->impressions);
print_r(" Clicks: %d\n", $campaign->campaignStats->clicks);
print_r(" Cost: $%.2f\n", $campaign->campaignStats->cost->microAmount / AdWordsConstants::MICROS_PER_DOLLAR);
print_r(" 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) {
print_r("An error has occurred: %s\n", $e->getMessage());
}
您需要从CLI运行它。 单击“开始并运行”,(出现黑屏)在命令行界面中键入此内容。
C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\AAAMYBUILD\timenow.php"
如果第一个位置是您的php.exe文件,并且第二个位置是您要运行的实际文件,则它将运行该文件。
您将在此窗口(而不是浏览器)中看到结果。
看来您应该能够在浏览器中运行这些文件,但我也从未做到过!
这是因为此示例希望您通过命令行运行它。 如果要在浏览器中运行它,则可以删除代码的以下部分:
// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.