PHP Client Library via Composer incomplete?

943 views
Skip to first unread message

Martin Roettgerding

unread,
Jun 7, 2017, 8:35:04 AM6/7/17
to AdWords API Forum
Hi,

after installing the current PHP library for v201705 via Composer I seem to be missing a few classes and files. For example, /src/Google/AdsApi/AdWords/AdWordsServices.php is missing, resulting in an error when using AdWordsServices (Fatal error: Class 'Google\AdsApi\AdWords\AdWordsServices' not found). On Github, the file seems to be present: https://github.com/googleads/googleads-php-lib/blob/master/src/Google/AdsApi/AdWords/AdWordsServices.php

The examples on Github also seem different from the ones I have on my file system. For example, this one: https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201705/BasicOperations/GetCampaigns.php

On Github the file looks like this:
<?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\v201705\BasicOperations;
require '../../../../vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201705\cm\CampaignService;
use Google\AdsApi\AdWords\v201705\cm\OrderBy;
use Google\AdsApi\AdWords\v201705\cm\Paging;
use Google\AdsApi\AdWords\v201705\cm\Selector;
use Google\AdsApi\AdWords\v201705\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example gets all campaigns. To add a campaign, run AddCampaign.php.
*/
class GetCampaigns {
 const PAGE_LIMIT = 500;
 public static function runExample(AdWordsServices $adWordsServices,
     AdWordsSession $session) {
   $campaignService = $adWordsServices->get($session, CampaignService::class);
   // Create selector.
   $selector = new Selector();
   $selector->setFields(['Id', 'Name']);
   $selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
   $selector->setPaging(new Paging(0, self::PAGE_LIMIT));
   $totalNumEntries = 0;
   do {
     // Make the get request.
     $page = $campaignService->get($selector);
     // Display results.
     if ($page->getEntries() !== null) {
       $totalNumEntries = $page->getTotalNumEntries();
       foreach ($page->getEntries() as $campaign) {
         printf(
             "Campaign with ID %d and name '%s' was found.\n",
             $campaign->getId(),
             $campaign->getName()
         );
       }
     }
     // Advance the paging index.
     $selector->getPaging()->setStartIndex(
         $selector->getPaging()->getStartIndex() + self::PAGE_LIMIT);
   } while ($selector->getPaging()->getStartIndex() < $totalNumEntries);
   printf("Number of results found: %d\n", $totalNumEntries);
 }
 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);
 }
}
GetCampaigns::main();


The file I have:
<?php
/**
 * This example gets all campaigns in the account. To add a campaign, run
 * AddCampaign.php.
 *
 * Copyright 2016, 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.
 *
 * @package    GoogleApiAdsAdWords
 * @subpackage v201705
 * @category   WebServices
 * @copyright  2016, Google Inc. All Rights Reserved.
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License,
 *             Version 2.0
 */

// Include the initialization file
require_once dirname(dirname(__FILE__)) . '/init.php';

/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 */
function GetCampaignsExample(AdWordsUser $user) {
  // Get the service, which loads the required classes.
  $campaignService = $user->GetService('CampaignService', ADWORDS_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, AdWordsConstants::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 += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
  } while ($page->totalNumEntries > $selector->paging->startIndex);
}

// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  return;
}

try {
  // Get AdWordsUser from credentials in "../auth.ini"
  // relative to the AdWordsUser.php file's directory.
  $user = new AdWordsUser();

  // Log every SOAP XML request and response.
  $user->LogAll();

  // Run the example.
  GetCampaignsExample($user);
} catch (Exception $e) {
  printf("An error has occurred: %s\n", $e->getMessage());
}


The file I have is identical to the ones from v201702, v201609, and v201607, except for line 21 (@subpackage), which always refers to the respective version.

I admit I am new to working with Composer. Since I reinstalled everything to work with v201705 I was under the impression that things should work smoothly. Did I miss something very basic?

Thanks

Martin

Shwetha Vastrad (AdWords API Team)

unread,
Jun 7, 2017, 12:57:20 PM6/7/17
to AdWords API Forum
Hi, 

A new ads php client library was released in December 2016. In this library you need construct an API session using the AdWordsSessionBuilder.php and obtain a service from the ads services factory, specifying the service class using ::class after it has been imported. It looks like the example you have in your system belongs to the old library which has been moved to a deprecated branch. Please try using the GetCampaigns.php example from the master branch and let me know if it works. Please see this upgrading guide for more details. 

Regards,
Shwetha, AdWords API Team.

Martin Roettgerding

unread,
Jun 7, 2017, 4:08:52 PM6/7/17
to AdWords API Forum
Hi Shwetha,

Thanks for your reply!

I had already made the upgrade to the new libraries a few months ago. However, I don't think the problem is my code as I can't even get the official examples to run.

I tried to start from scratch. Here's what I did:
  • I got composer and ran the following from command line:
  • php composer.phar require googleads/googleads-php-lib
  • cat vendor/googleads/googleads-php-lib/examples/AdWords/v201705/BasicOperations/GetCampaigns.php
The result is the example from the old library:
This is apparently not the file from the master branch. But shouldn't php composer.phar require googleads/googleads-php-lib have gotten me everything from the master branch?

Thanks & sorry if I'm just missing something...
Martin

Shwetha Vastrad (AdWords API Team)

unread,
Jun 8, 2017, 10:17:19 AM6/8/17
to AdWords API Forum
Hi Martin,

Could you provide the details of the composer.json so I can take a look at the version of the googleads-php-lib package used in your project? You could post your question on the library's issue tracker as well.

Thanks,
Shwetha, AdWords API Team.
Message has been deleted

Martin Roettgerding

unread,
Jun 8, 2017, 10:24:05 AM6/8/17
to AdWords API Forum
Hi Shwetha,

This is the entire composer.json:

{
    "require": {
        "googleads/googleads-php-lib": "^18.1"
    }
}


Does that help?

Thanks
Martin

Martin Roettgerding

unread,
Jun 8, 2017, 10:35:26 AM6/8/17
to AdWords API Forum
Just realized there's a second composer.json in vendor/googleads/googleads-php-lib:

{
    "name": "googleads/googleads-php-lib",
    "description": "Google Ads APIs Client Library for PHP (AdWords and DFP)",
    "require": {
        "php": ">=5.3.0",
        "ext-curl": "*",
        "ext-openssl": "*",
        "ext-soap": "*"
    },
    "type": "library",
    "license": "Apache-2.0",
    "include-path": ["src/"],
    "autoload": {
      "classmap": [
        "src/Google/Api/Ads/AdWords/Lib",
        "src/Google/Api/Ads/Dfp/Lib"
      ]
    },
    "authors": [
      {
        "name": "Google",
      }
    ]
}

Shwetha Vastrad (AdWords API Team)

unread,
Jun 8, 2017, 2:08:27 PM6/8/17
to AdWords API Forum
Hi Martin, 

The googleads-php-lib version 18.1 is a release of the old ads PHP client library. Please use the examples available here with this version of the client library. To get the new version of the client library, please update the first composer.json as shown below and then install the client library:

"require": {
     
"googleads/googleads-php-lib": "^25.0.0"
 
}

Martin Roettgerding

unread,
Jun 8, 2017, 5:54:10 PM6/8/17
to AdWords API Forum
Wow, that was easy. It worked and everything is working again.

Thanky you, Shwetha!

Reply all
Reply to author
Forward
0 new messages