googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
288 stars 260 forks source link

Codeigniter Integration #965

Closed Stecnica closed 8 months ago

Stecnica commented 10 months ago

Hello, after installed google-ads-php library in and Ubuntu server 22 system, building the solution via ssh with composer and getting the vendor folder, I am getting the api response from the terminal by typing

php examples/BasicOperations/GetCampaigns.php --customerId 123456789

Now my goal would be to integrate the solution into Codeigniter3. But it is not clear to me how to use the vendor folder.

I tried the old system to make API available to a Codeigniter controller; -Copied the entire google-ads-php folder inside the _/Application/Thirdparty/ folder of codeigniter; -Created a '_google_adsphp.php'* library inside the /application/libraries/ folder of codeigniter; -Created a '_googleads.php'** controller that uses the class specified in the library with _$ this -> Load -> Library ('google_adsphp.php');.

I can't get the desired result

Is anybody trying to build reporting application using ads api facing the same issue? Please Help!.

define ( 'GAP_PATH' , APPPATH.'third_party/google-ads-php/' ) ; define ( 'SRC_PATH' , GAP_PATH.'src/' ) ; define ( 'LIB_PATH' , 'Google/Ads/GoogleAds/Lib' ) ; define ( 'UTIL_PATH' , 'Google/Ads/GoogleAds/Util' ) ;

define ( 'google_ads_VERSION' , 'V14' ) ;

// Configure include path ini_set ( 'include_path' , implode ( array ( ini_get ( 'include_path' ) , PATH_SEPARATOR , SRC_PATH ) ) ) ;

require_once SRC_PATH.LIB_PATH.'/'.google_ads_VERSION.'/GoogleAdsClient.php' ;

class Google_ads_php extends GoogleAdsClient { public function construct ( ) { parent::construct ( ) ; }

function GetCampaigns ( ) {
    // Get the service , which loads the required classes.
    $campaignService = $this -> GetService ( 'CampaignService' , google-ads_VERSION ) ;

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

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

    do {
        // Make the get request.
        $page = $campaignService -> get ( $selector ) ;
        // Display results
        if ( isset ( $page -> entries ) ) {
            foreach ( $page -> entries as $campaign ) {
                printf ( "Campaign with name '%s' and ID '%s' was found.\n" ,
                        $campaign -> name , $campaign -> id ) ;
            }
        } else {
            print "No campaigns were found.\n" ;
        }

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

}

** Controller

class Google_ads extends CI_Controller {

public function __construct ( ) {
    parent::__construct ( ) ;
    $this -> load -> library ( 'google_ads_php' ) ;
}

public function google ( ) {
    $data = $this -> inizializza ( ) ;
    $data [ 'user' ] = new Google_ads ( ) ;
    $data [ 'user' ] -> GetCampaigns ( ) ;
}

}

fiboknacky commented 9 months ago

The way this library uses the vendor folder should be the same as other PHP libraries. Would it be possible to seek help in CodeIgniter forum too? Unfortunately, we have an example of Laravel only.

gl2007 commented 9 months ago

I have this library working inside of a CodeIgniter app. What I have done is copy out examples that I am interested into Controllers folder and modified as necessary for e.g. converting the examples to use classes extending from some base controller I have.


<?php

/**
 * Copyright 2023 Google LLC
 *
 * 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
 *
 *     https://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\Ads\GoogleAds\Examples\Planning;
namespace App\Controllers;
use App\Helpers\MySqlDb;
use DateTime;

if (!defined('FilePath')) {
    define("FilePath", realpath( dirname( __FILE__ ) ).'/' );
}

require_once FilePath.'../../vendor/autoload.php';
require_once FilePath.'../Helpers/common.php';

use GetOpt\GetOpt;
///use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
//use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V15\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V15\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V15\GoogleAdsException;
use Google\Ads\GoogleAds\Util\V15\ResourceNames;
use Google\Ads\GoogleAds\V15\Common\MonthlySearchVolume;
use Google\Ads\GoogleAds\V15\Enums\KeywordPlanCompetitionLevelEnum\KeywordPlanCompetitionLevel;
use Google\Ads\GoogleAds\V15\Enums\KeywordPlanNetworkEnum\KeywordPlanNetwork;
use Google\Ads\GoogleAds\V15\Enums\MonthOfYearEnum\MonthOfYear;
use Google\Ads\GoogleAds\V15\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V15\Services\GenerateKeywordHistoricalMetricsRequest;
use Google\Ads\GoogleAds\V15\Services\GenerateKeywordHistoricalMetricsResult;
use Google\ApiCore\ApiException;

class GoogleAdsXYZ extends MyBaseController
{
...
}

This works for me after I use Composer to bring in the dependencies.

fiboknacky commented 8 months ago

Thanks @gl2007. Since there is no further question for a while, let me close this issue for now.