How to include adWords in a cakePhp Model to be able to use its classes?

放肆的年华 提交于 2019-12-13 03:42:20

问题


I'm trying to use AdWords API from Google in a cakePHP application. I need to search for keywords at some point inside an already existing model that I made.

The idea is to get information about the keyword if I dont' already got it in my database. But when I need to use some classes of adWords, I'm getting this error :

Error: Class 'TargetingIdeaSelector' not found
File: C:\wamp64\www\projet\app\Model\Keyword.php

Line: 36

Here is an idea of the code I have :

public function getsuggestions($word, $someData) {

        if(!empty($word) && $word != '') {
            $data = $this->find('all', array(
                'conditions' => array('Keyword.mot_saisi' => $word)
            ));
            if(!empty($data)) { 
                $motcles = split(",",$data['0']['Keyword']['result'],11);
                $motcles['10'] = $word;

                return $motcles;
            }
            else {
                // Create selector.
                $selector = new TargetingIdeaSelector(); //Here is the error
                $selector->requestType = 'IDEAS';
                $selector->ideaType = 'KEYWORD';
                $selector->requestedAttributeTypes = array('KEYWORD_TEXT', 
'SEARCH_VOLUME',
                    'CATEGORY_PRODUCTS_AND_SERVICES');
                // Create language search parameter (optional).
                // The ID can be found in the documentation:
                //   
https://developers.google.com/adwords/api/docs/appendix/languagecodes
                // Note: As of v201302, only a single language parameter is 
allowed.
                $languageParameter = new LanguageSearchParameter();
                $french = new Language();
                $french->id = 1002;
                $languageParameter->languages = array($french);
                // Create related to query search parameter.
                $relatedToQuerySearchParameter = new 
RelatedToQuerySearchParameter();
                $relatedToQuerySearchParameter->setQueries(array($word));
                $selector->searchParameters[] = 
$relatedToQuerySearchParameter;
                $selector->searchParameters[] = $languageParameter;
                // Set selector paging (required by this service).
                $paging = new Paging();
                $paging->setStartIndex(0);
                $paging->setNumberResults(10);
                $selector->setPaging($paging);

                // Make the get request.
                $content = $targetingIdeaService->get($selector);

[...]

I guess adWords classes are not included in my model, but how can I include all the adWords content so that I can use multiple classes inside my model ?

(I have googleads-php-lib directly inside my project)

EDIT

Here is the exact solution :

require_once __DIR__ . '../../Vendor/autoload.php';
use Google\AdsApi\AdWords\v201710\o\TargetingIdeaSelector;
use Google\AdsApi\AdWords\v201710\o\LanguageSearchParameter;
use Google\AdsApi\AdWords\v201710\o\RelatedToQuerySearchParameter;
use Google\AdsApi\AdWords\v201710\cm\Language;
use Google\AdsApi\AdWords\v201710\cm\Paging;

回答1:


(I have googleads-php-lib directly inside my project)

You need to tell the auto loader where to find it and use the use statement inside your file.

  • http://php.net/manual/en/language.namespaces.importing.php
  • https://getcomposer.org/doc/01-basic-usage.md#autoloading
  • PHP namespaces and "use"
  • Custom code management with the Composer auto loader?
  • PHP adding custom namespace using autoloader from composer
  • https://jtreminio.com/2012/10/composer-namespaces-in-5-minutes/


来源:https://stackoverflow.com/questions/50605057/how-to-include-adwords-in-a-cakephp-model-to-be-able-to-use-its-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!