AdWords API PHP问题

时间:2013-12-26 20:02:17

标签: php soap google-adwords

我遇到一些实际问题,弄清楚我的代码在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());
}

2 个答案:

答案 0 :(得分:0)

您需要从CLI运行此命令。 点击开始并运行,(你得到一个黑屏) 在命令行界面中输入此内容。

C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\AAAMYBUILD\timenow.php"

如果第一个位置是您的php.exe文件,并且第二个位置是您要运行的实际文件,则会运行该文件。

您将在此窗口中看到结果,而不是浏览器。

您似乎应该能够在浏览器中运行这些文件,但我也从未能够这样做!

答案 1 :(得分:0)

这是因为此示例希望您通过命令行运行它。如果要在浏览器中运行它,可以删除代码的以下部分:

// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
    return;
}