简体   繁体   English

AdWords API PHP问题

[英]AdWords API PHP Issues

I am having some real problems figuring out what is wrong with my code for use with the Adwords API in php. 我在找出与PHP中的Adwords API一起使用的代码有什么问题时遇到了一些实际问题。 I am trying to retrieve simple campaign stats for entering into a local database. 我正在尝试检索简单的广告系列统计信息,以输入本地数据库。 However, I am unable to make this happen. 但是,我无法做到这一点。 I am using the examples from the initial download, however return a completely blank screen with no useful error messages at all. 我使用的是最初下载的示例,但是返回的是一个完全空白的屏幕,根本没有任何有用的错误消息。 Below is my code, I'm hoping somebody here has run across this and can help me solve this. 下面是我的代码,我希望这里有人遇到过这个问题,可以帮助我解决这个问题。

    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());
}

You need to run this from the CLI. 您需要从CLI运行它。 Click start and run, (you get a black screen) Type this into your command line interface. 单击“开始并运行”,(出现黑屏)在命令行界面中键入此内容。

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

This will run the file if the first location is your php.exe file, and if this second location is the actual file you want to run. 如果第一个位置是您的php.exe文件,并且第二个位置是您要运行的实际文件,则它将运行该文件。

You will see results in this window, never a browser. 您将在此窗口(而不是浏览器)中看到结果。

It does seem that you should be able to run these files in a browser but I have never been able to do that either! 看来您应该能够在浏览器中运行这些文件,但我也从未做到过!

It is because this example expects you to run it through a command line. 这是因为此示例希望您通过命令行运行它。 If you want to run it in your browser you can remove the following part of your code: 如果要在浏览器中运行它,则可以删除代码的以下部分:

// 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