Google AdWords and PHP 5’s Native Soap

By on April 26, 2007 5:48 pm

While I’m sure there are plenty of others who have had success ridding themselves of apility, very few places describe how this can be accomplished. I initially found an old post which described the basics of connecting directly to Google AdWords with the native Soap support in PHP. What I needed to create was a very lightweight library for communicating with the various ad networks. I didn’t want to define full class structures for each of the many, many types that the various ad networks have. So I set off to create a simple set of libraries for communicating with each network using PHP 5’s native Soap capabilities.

MSN, no problem…done.

Yahoo, no problem…done.

Google, no proble..err wait almost no problem.

Google went along smoothly as with the other networks until I hit a roadblock. In a few circumstances, Google uses inheritance with their complexTypes. For example, a TextAd is an extension of an Ad. In my simple code, I was creating an associative array with the appropriate structure for a TextAd and sending it off to __soapCall() as the parameter. In this particular case, I noticed (by receiving exceptions from Google) that only the properties that were part of the Ad were being encoded in the Soap envelope. The properties that were specific to TextAd were getting dropped.

After a couple hours of searching and experimentation, I found little that seemed to point me in the correct direction. I had tried to use the SoapVar, but there was very little documentation and fewer examples. However, I finally stumbled upon the relatively simple answer and here I am to document it so that the next poor soul traveling down this path will get there faster than I did.

Starting with an array like this:

[id] => 0
[adType] => TextAd
[headline] => Javascript and Ajax Training
[description1] => Javascript and Ajax Training
[description2] => by the founders of the DojoToolkit
[destinationUrl] =>
[displayUrl] =>
[adGroupId] => 123423414
[disapproved] => 0
[status] => Enabled

You can simply force it to be encoded as a particular complexType from your wsdl like this:

 new SoapVar($adArray,0, "TextAd",""); 

Now that wasn’t too bad, was it?