Adwords API 500 Internal Server Error in GetKeywordIdeas

64 views
Skip to first unread message

Sang Jin Kim

unread,
May 7, 2018, 2:22:15 PM5/7/18
to AdWords API and Google Ads API Forum
Hi,

I am using Adwords API to manage many adwords accounts, and works fine for another apis(ex. Basic Operations like AddCampaigns, Reporting like Download a criteria performance report)

But when I try to get keyword ideas through API, I always receive '500 Internal Server Error' result. I have same error even though I changed to other ClientCustomerId.


I'm in South Korea, and I use v201710 API version.

Below is my test script. 

Thanks in advance!


--------------------------------------------------------------------------

<?php

/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Google\AdsApi\Examples\AdWords\v201710\Optimization;

require '/var/www/html/ad_manager/google_v201710/googleads/googleads-php-lib/vendor/autoload.php';

use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201710\cm\Language;
use Google\AdsApi\AdWords\v201710\cm\NetworkSetting;
use Google\AdsApi\AdWords\v201710\cm\Paging;
use Google\AdsApi\AdWords\v201710\o\AttributeType;
use Google\AdsApi\AdWords\v201710\o\IdeaType;
use Google\AdsApi\AdWords\v201710\o\LanguageSearchParameter;
use Google\AdsApi\AdWords\v201710\o\NetworkSearchParameter;
use Google\AdsApi\AdWords\v201710\o\RelatedToQuerySearchParameter;
use Google\AdsApi\AdWords\v201710\o\RequestType;
use Google\AdsApi\AdWords\v201710\o\SeedAdGroupIdSearchParameter;
use Google\AdsApi\AdWords\v201710\o\TargetingIdeaSelector;
use Google\AdsApi\AdWords\v201710\o\TargetingIdeaService;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\Common\Util\MapEntries;

/**
 * This example gets keyword ideas related to a seed keyword.
 */
class GetKeywordIdeas {

  // If you do not want to use an existing ad group to seed your request, you
  // can set this to null.
  const AD_GROUP_ID = null;

  const PAGE_LIMIT = 500;

  public static function runExample(AdWordsServices $adWordsServices, AdWordsSession $session, $adGroupId, $seed_keyword) {

    $targetingIdeaService = $adWordsServices->get($session, TargetingIdeaService::class);

    // Create selector.
    $selector = new TargetingIdeaSelector();
    $selector->setRequestType(RequestType::IDEAS);
    $selector->setIdeaType(IdeaType::KEYWORD);
    $selector->setRequestedAttributeTypes([
        AttributeType::KEYWORD_TEXT,
        AttributeType::SEARCH_VOLUME,
        AttributeType::AVERAGE_CPC,
        AttributeType::COMPETITION,
        AttributeType::CATEGORY_PRODUCTS_AND_SERVICES
    ]);

    $paging = new Paging();
    $paging->setStartIndex(0);
    $paging->setNumberResults(10);
    $selector->setPaging($paging);

    $searchParameters = [];
    // Create related to query search parameter.
    $relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
    $relatedToQuerySearchParameter->setQueries([$seed_keyword]);
    $searchParameters[] = $relatedToQuerySearchParameter;


    //// Create language search parameter (optional).
    //// The ID can be found in the documentation:
    //$languageParameter = new LanguageSearchParameter();
    //$korean = new Language();
    //$korean->setId(1012);
    //$languageParameter->setLanguages([$korean]);
    //$searchParameters[] = $languageParameter;


    // Create network search parameter (optional).
    $networkSetting = new NetworkSetting();
    $networkSetting->setTargetGoogleSearch(true);
    $networkSetting->setTargetSearchNetwork(false);
    $networkSetting->setTargetContentNetwork(false);
    $networkSetting->setTargetPartnerSearchNetwork(false);

    $networkSearchParameter = new NetworkSearchParameter();
    $networkSearchParameter->setNetworkSetting($networkSetting);
    $searchParameters[] = $networkSearchParameter;


    //// Optional: Use an existing ad group to generate ideas.
    //if (!empty($adGroupId)) {
      //$seedAdGroupIdSearchParameter = new SeedAdGroupIdSearchParameter();
      //$seedAdGroupIdSearchParameter->setAdGroupId($adGroupId);
      //$searchParameters[] = $seedAdGroupIdSearchParameter;
    //}
    //$selector->setSearchParameters($searchParameters);
    //$selector->setPaging(new Paging(0, self::PAGE_LIMIT));



    // Get keyword ideas.
    $page = $targetingIdeaService->get($selector);


    // Print out some information for each targeting idea.
    $entries = $page->getEntries();

    if ($entries !== null) {

      foreach ($entries as $targetingIdea) {

        $data = MapEntries::toAssociativeArray($targetingIdea->getData());

        $keyword = $data[AttributeType::KEYWORD_TEXT]->getValue();

        $searchVolume = ($data[AttributeType::SEARCH_VOLUME]->getValue() !== null) ? $data[AttributeType::SEARCH_VOLUME]->getValue() : 0;

        $averageCpc = $data[AttributeType::AVERAGE_CPC]->getValue();
        $competition = $data[AttributeType::COMPETITION]->getValue();
        $categoryIds = ($data[AttributeType::CATEGORY_PRODUCTS_AND_SERVICES]->getValue() === null) ? $categoryIds = '' : implode( ', ', $data[AttributeType::CATEGORY_PRODUCTS_AND_SERVICES]->getValue() );

printf(
            "Keyword with text '%s', average monthly search volume %d, "
                . "average CPC %d, and competition %.2f "
                . "was found with categories: %s\n",
            $keyword,
            $searchVolume,
            ($averageCpc === null) ? 0 : $averageCpc->getMicroAmount(),
            $competition,
            $categoryIds
        );
      }
    }

    if (empty($entries)) {
      print "No related keywords were found.\n";
    }
  }




  public static function main($clientcustomerid, $seed_keyword) {

    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile('/var/www/html/ad_manager/google_v201710/googleads/googleads-php-lib/examples/AdWords/adsapi_php.ini')
        ->build();

    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new AdWordsSessionBuilder())
        ->fromFile('/var/www/html/ad_manager/google_v201710/googleads/googleads-php-lib/examples/AdWords/adsapi_php.ini')
        ->withOAuth2Credential($oAuth2Credential)
->withClientCustomerId($clientcustomerid)
        ->build();

    self::runExample(new AdWordsServices(), $session, self::AD_GROUP_ID, $seed_keyword);

  }

}



$seed_keyword = '다이어트';

$clientcustomerid = '000-000-0000'; //test clientcustomerid 

GetKeywordIdeas::main($clientcustomerid, $seed_keyword);

--------------------------------------------------------------------------


Sreelakshmi Sasidharan (AdWords API Team)

unread,
May 7, 2018, 5:38:36 PM5/7/18
to AdWords API and Google Ads API Forum
Hi Sang, 

I see that you have commented out the lines where SearchParameters and Paging are being set in the selector (lines given below). Could you please un-comment those and try again? If you are still facing issues, could you please enable logging and share the SOAP logs (request/response)? You could opt to reply privately to author while sharing the logs. 

$selector->setSearchParameters($searchParameters);
$selector->setPaging(new Paging(0, self::PAGE_LIMIT));

Thanks,
Sreelakshmi, AdWords API Team

kim sang jin

unread,
May 8, 2018, 5:01:29 PM5/8/18
to AdWords API and Google Ads API Forum

I uncommented some codes as you pointed out and commented out some codes over namespace statement, it works fine!

Thanks a lot, Sreelakshmi ^^


P.S. 

I had tried in browser before, but this time I tried in command mode and give error message like below.

- PHP Fatal error:  Namespace declaration statement has to be the very first statement in the script in ...

After comment out some codes over namespace, works fine!




2018년 5월 8일 화요일 오전 6시 38분 36초 UTC+9, Sreelakshmi Sasidharan (AdWords API Team) 님의 말:

Sreelakshmi Sasidharan (AdWords API Team)

unread,
May 9, 2018, 10:20:22 AM5/9/18
to AdWords API and Google Ads API Forum
Hi Sang, 

Glad to know that you got it working. 
Reply all
Reply to author
Forward
0 new messages