3
votes

I'm using Google Adwords API (v201109) to get the keywords and their search volumes. I have created the file

adwords.php with the following code -

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$path = dirname(__FILE__) . '/../../../';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';

$adwords_username = "myusername@gmail.com";
$adwords_password = "mypassword";
$adwords_developerToken = "mydevelopertoken";
$user = new AdWordsUser(null, $adwords_username, $adwords_password, $adwords_developerToken);
$user->SetDefaultServer("https://adwords.google.com/");

//set Adwords Client Id
$user->SetClientCustomerId('111111111');

$str_group = GetAdGroup($user, "v201509");
echo $str_group;

function GetAdGroup(AdWordsUser $user, $adwords_version) {
    // Get the service, which loads the required classes.
    $adgroupService = $user->GetService('AdGroupService', $adwords_version);

    // Create selector.
    $selector = new Selector();
    $selector->fields = array('Id', 'Name', 'CampaignId', 'Status');
    $selector->ordering[] = new OrderBy('CampaignId', 'ASCENDING');

    // Filter out deleted criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED', 'PAUSED'));

    // Create paging controls.
    $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

    $ret = array();
    do {
    //echo "<pre>";print_r($selector);die;
        // Make the get request.
        $page = $adgroupService->get($selector);

        // Display results.
        if (isset($page->entries)) {
            foreach ($page->entries as $adgroup) {
                //printf("AdGroup with name '%s' and id '%s' was found for Campaign: '%s' and Status: '%s'\n",
                // $adgroup->name, $adgroup->id, $adgroup->campaignId, $adgroup->status);
                $ret[] = array(
                    'name' => $adgroup->name,
                    'id' => $adgroup->id,
                    'campaignId' => $adgroup->campaignId,
                    'active' => (strcmp($adgroup->status,'ENABLED')==0)?1:0
                );
            }
        } else {
            //print "No adgroups were found.\n";
        }
        // Advance the paging index.
        $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
    } while ($page->totalNumEntries > $selector->paging->startIndex);
    return $ret;
}

function GetKeywords(AdWordsUser $user, $adGroupId, $days, $adwords_version) {
    // Get the service, which loads the required classes.
    $adGroupCriterionService = $user->GetService('AdGroupCriterionService', $adwords_version);

    // Create selector.
    $selector = new Selector();

    $selector->fields = array('KeywordText', 'KeywordMatchType', 'Id', 'AverageCpc', 'AveragePosition', 'Clicks', 'Conversions', 'Cost', 'Ctr', 'Impressions', 'QualityScore', 'Status');
    $selector->ordering[] = new OrderBy('KeywordText', 'ASCENDING');

    // Create predicates.
    $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId));
    $selector->predicates[] = new Predicate('CriteriaType', 'IN', array('KEYWORD'));
    $selector->predicates[] = new Predicate('Status', 'IN', array('ACTIVE'));
    $selector->predicates[] = new Predicate('Impressions', 'GREATER_THAN', array('1'));

    $dateRange = new DateRange();
    $str1 = '-1 days';
    $str2 = '-1 days';
    if ($days > 0) {
    $str1 = '-' . $days . ' days';
    $str2 = '-' . $days . ' days';
    }
    $dateRange->min = date('Ymd', strtotime($str1));
    $dateRange->max = date('Ymd', strtotime($str2));
    $selector->dateRange = $dateRange;

    // Create paging controls.
    $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

    $ret = array();

    do {
    // Make the get request.
    $page = $adGroupCriterionService->get($selector);

    // Display results.
    if (isset($page->entries)) {
        foreach ($page->entries as $adGroupCriterion) {
        $cr = 0;
        if ($adGroupCriterion->stats->clicks > 0)
            $cr = $adGroupCriterion->stats->conversions / $adGroupCriterion->stats->clicks;

        $ret[] = array(
            'name' => $adGroupCriterion->criterion->text,
            'type' => $adGroupCriterion->criterion->matchType,
            'id' => $adGroupCriterion->criterion->id,
            'clicks' => $adGroupCriterion->stats->clicks,
            'cpc' => $adGroupCriterion->stats->averageCpc->microAmount / 1000000,
            'conversions' => $adGroupCriterion->stats->conversions,
            'cost' => $adGroupCriterion->stats->cost->microAmount / 1000000,
            'ctr' => $adGroupCriterion->stats->ctr,
            'impressions' => $adGroupCriterion->stats->impressions,
            'qualityfactor' => $adGroupCriterion->qualityInfo->qualityScore,
            'cr' => $cr,
            'position' => $adGroupCriterion->stats->averagePosition
        );
        }
    } else {
        //print "No keywords were found.\n";
    }

    // Advance the paging index.
    $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
    } while ($page->totalNumEntries > $selector->paging->startIndex);
    return $ret;
}

But I keep getting the following error when I run the adwords.php file.

Error

Fatal error: Uncaught SoapFault exception: [soap:Server] [QuotaCheckError.INVALID_TOKEN_HEADER @ ; trigger:'abhijeetk.aloha@gmail.com'] in /var/www/html/Google/Api/Ads/Common/Lib/AdsSoapClient.php:216 Stack trace: #0 /var/www/html/Google/Api/Ads/Common/Lib/AdsSoapClient.php(216): SoapClient->__soapCall('get', Array, NULL, Array, Array) #1 /var/www/html/Google/Api/Ads/AdWords/v201509/AdGroupService.php(7408): AdsSoapClient->__soapCall('get', Array) #2 /var/www/html/adwords.php(46): AdGroupService->get(Object(Selector))

3 /var/www/html/adwords.php(22): GetAdGroup(Object(AdWordsUser), 'v201509') #4 {main} thrown in

/var/www/html/Google/Api/Ads/Common/Lib/AdsSoapClient.php on line 216

I am new to this so I don't know how to use the APIs to get the data please tell me what I'm doing wrong. Or how can I use the adwords APIs to get the keywords and their volumes and other data. All the docs I found made me more confused.

Thanks in advance

2
What is the error that you get?bIgBoY
I recommend watching this tut youtube.com/watch?v=pPXYsSbXQDc it explains how to setup your auth.ini and get refresh token etc. You can test stuff with a test MCC and test account created with test MCC account. This stuff is pretty complex in the beginning, so it's normal to be overwhelmed.Robert Sinclair

2 Answers

0
votes
__construct($authenticationIniPath = null,
      $developerToken = null, $userAgent = null, $clientCustomerId = null,
      $settingsIniPath = null, $oauth2Info = null)

$user = new AdWordsUser(null, $adwords_username, $adwords_password, $adwords_developerToken);

Wrong data passed; Use auth.ini instead. https://developers.google.com/adwords/api/docs/first-request

0
votes

You are getting this error because you have not set your Developer Token or Your developer token still its in pending approval mode...

You can set your developer token into this file : \lib\Google\Api\Ads\AdWords\auth.ini