Skip to content

Instantly share code, notes, and snippets.

@stephenyeargin
Last active August 29, 2015 14:03
Show Gist options
  • Save stephenyeargin/01686190ccf770fe4714 to your computer and use it in GitHub Desktop.
Save stephenyeargin/01686190ccf770fe4714 to your computer and use it in GitHub Desktop.
Instructions of how to patch the googlesads-php-lib to work with with pseudo namespaces

HOWTO: Create a pseudo-namespaced version of the AdWords PHP library for v201402

History

While it is an ongoing project, the PHP library for AdWords does not yet fully make use of proper namespacing througout. Until that is complete, it may be advisable to use a "psedudo namespaced" version that simply prepends a bit of data to class names. Note that if you do it this way, you will want to use this format for creating objects. It's also a better practice anyway.

// Old
$tracker = new AdWordsConversionTracker();

// New
$conversionTrackerService = $this->adwords_user->GetService('ConversionTrackerService');
$tracker = $conversionTrackerService->Create('AdWordsConversionTracker');

I'm making the assumption that you are already using this library in production and trying to migrate it before the sunset deadline.

Setup

  1. Clone the current repository, e.g. cd /some/project/directory ; git clone git@github.com:googleads/googleads-php-lib.git.
  2. Install phing as described here. This is used to do the build task.

Patching the library

Next, you will need to patch the current WDSL with a previous tagged version (that uses pseudo namespacing instead of the partial namespacing support.) These instructions via @saturnism on the related issue.

$ cd /path/to/downloaded-repository

# make a new branch for pseudo-namespace
$ git checkout 5.3.0
$ git checkout -b 5.3.0-pns

# replace build_lib w/ older version
$ cd build_lib
$ rm -rf *
$ git checkout 5.2.3 .

Enabling Pseudo Namespaces

With that now patched, you have a few more edits to make in the build process. Within the <project> tag, around line 7, add this line in order to properly tell phing where to build from.

<project basedir="../" default="generate-src" name="adsapi_php">

  <property file="src/Google/Api/Ads/AdWords/Lib/api.properties" />
  <property file="src/Google/Api/Ads/Dfp/Lib/api.properties" />

  <property name="classpath" value="" />
[...]

Next, you will want to tell phing to use the psedo namespaces. Edit src/Google/Api/Ads/AdWords/Lib/api.properties to add:

;; Patched to use 5.2.3 declaration
wsdl2php.enablePseudoNamespaces=True

With these in place, you should now be able to run the builder.

$ cd build_dir
$ phing

This will kick off the generation process that will take a minute or so.

Fixing the SOAP client

There will be a few issues with using the project as-is because of some improper class naming in the build script. The first error will say that it is unable to find the Google_Api_Ads_AdWords_Lib_AdWordsSoapClient class.

Do a mass find/replace for the following in your project folder:

extends Google_Api_Ads_AdWords_Lib_AdWordsSoapClient

to

extends AdWordsSoapClient

The next time it will complain about not be able to find the native classes (as they are called through GetService). You will need to patch Google/Api/Ads/Common/Lib/SoapClientFactory.php (also via @saturnism).

public function GenerateSoapClient($serviceName) {
    ...
    $this->DoRequireOnce($serviceName);
    // Add the following line, assuming you only use the AdWords components
    $serviceName = 'Google_Api_Ads_AdWords_' . $this->GetVersion() . '_' . $serviceName;

    $soapClient = ...
    ...
}

At this point, your library is patched to use the new v201402 version. Copy the relevant files into your project and start working through the other changes outlined in the migration guide.

Built branch

This fork of the code has followed the instructions if you just want to quickly get going:

https://github.com/stephenyeargin/googleads-php-lib/tree/5.3.0-pns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment