dsa89029 2017-12-08 20:49
浏览 70
已采纳

Adwords API修改类以传递变量并在变量中获取响应

I'm trying to modify a class given from a PHP script example from the Adwords API.

I'm pretty new to playing with Objects in PHP so I can't figure out how to make it work.

I would like to modify this script below in order to pass on a keyword array as a variable, therefore replacing the keywords contained in:

$relatedToQuerySearchParameter->setQueries([
        'bakery', 'pastries', 'birthday cake'
    ]);

Maybe to use it as a function will normally do :

function GetKeywordIdeas($array_keywords){...}

And then get the results in a variable

Here's the code from the example:

<?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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 __DIR__ . '/../../../../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 = 'INSERT_AD_GROUP_ID_HERE';
  const PAGE_LIMIT = 500;

  public static function runExample(AdWordsServices $adWordsServices,
      AdWordsSession $session, $adGroupId) {
    $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([
        'bakery', 'pastries', 'birthday cake'
    ]);
    $searchParameters[] = $relatedToQuerySearchParameter;

    // Create language search parameter (optional).
    // The ID can be found in the documentation:
    // https://developers.google.com/adwords/api/docs/appendix/languagecodes
    $languageParameter = new LanguageSearchParameter();
    $english = new Language();
    $english->setId(1000);
    $languageParameter->setLanguages([$english]);
    $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
",
            $keyword,
            $searchVolume,
            ($averageCpc === null) ? 0 : $averageCpc->getMicroAmount(),
            $competition,
            $categoryIds
        );
      }
    }

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

  public static function main() {
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile()
        ->build();

    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new AdWordsSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();
    self::runExample(new AdWordsServices(), $session, self::AD_GROUP_ID);
  }
}

GetKeywordIdeas::main();

Thanks for your help or explanations.

  • 写回答

2条回答 默认 最新

  • dpblwmh5218 2017-12-08 21:20
    关注

    you need to change 4 lines in the code. 1.

      public static function main() {
    

    to

    public static function main($array_keywords) {
    

    and 2.

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

    to

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

    and 3.

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

    to

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

    and 4.

        $relatedToQuerySearchParameter->setQueries([
        'bakery', 'pastries', 'birthday cake'
    ]);
    

    to

        $relatedToQuerySearchParameter->setQueries($array_keywords);
    

    then you can call it like this:

    GetKeywordIdeas::main($array_keywords);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!