簡體   English   中英

AdWords API PHP問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM