Section 14.1. Using the AdWords API with PHP

14.1. Using the AdWords API with PHP

The web application in this chapter is programmed using PHP. PHP is most commonly used for programming in web applications. PHP pages on the Web are HTML pages with PHP code embedded in the HTML. This makes PHP great for web programmingafter all, it is used by more than 20 million web domainsbut not always the best language choice for all applications.

When the first version of PHP was created by Rasmus Lerdorf in the early 1990s, PHP was short for "Personal Home Page." As PHP has become a tool used by serious programmers on the Web, the meaning of the acronym has also changed; PHP now stands for "PHP: Hypertext Preprocessor."

Case in point: as you'll come to understand as you follow along with the code listings in this chapter, PHP does not interact easily with the document literal style of SOAP web services offered by the Google AdWords API. This means you'll have to go through some contortions to make PHP play nicely with the AdWords API.

Document Literal Versus RPC SOAP

The web services that make up the AdWords API are document literal web services, meaning that they work by processing XML documents. Formally, the input and output of a document literal web service is defined by an XML schema, and the body of the SOAP message (the input or output) can be any XML document that will validate with the schema.

In contrast, the body of an RPC (remote procedure call) SOAP message contains an element with the name of the method or remote procedure being invoked. This element in turn contains an element for each argument of that procedure.

Several other languages and development environments do a good job of encapsulating the messiness of document literal SOAP from programmers who are writing applications that consume these web services. Specifically, Visual Studio.NETand one of the .NET languages such as VB or C#or Java (combined with the Apache Axis library) make it very easy to program with the Google AdWords API. These tools hide the details of the SOAP interaction by generating proxy classes. The programmer can focus on working with the high-level objects instantiated from these classes, rather than worrying about the details of the SOAP handshake.

Document literal SOAP support is baked into Visual Studio.NET. For information about document literal SOAP support with Axis, see If you plan to work with Java/Axis, you should know that Google makes available a client library in a single JAR file that contains all the Axis JARs and precompiled stub classes needed to write Java clients. You can download this library from

To summarize, if .NET or Java/Axis works well with your other systems, perhaps because you or your business is already programming in one of these environments, then these languages would be the best choice for working with the Google AdWords API.

Despite the ease of using these other languages with AdWords, I've chosen to show the examples in this book in PHP, which is a nice language for web development, even if it's not so good for working with the AdWords API. If I had written the examples using C# .NET or Java, it wouldn't have been as useful to readers unfamiliar with these languages and their development environments. PHP is accessible to almost any programmer. It's free (well, so is Java!) and it runs on every flavor of operating system, so no one is ruled out. And PHP is as good as any other environment for learning about the different services that comprise the AdWords API, the classes that comprise the services, and how they interact.

14.1.1. SOAP Libraries for PHP

SOAP libraries , also called SOAP toolkits, are used to interpret WSDL files (see Chapter 13 for information about WSDL files). These libraries also provide tools that help encode and decode XML request and response messages.

The two SOAP libraries available for use with PHP are PEAR and NuSOAP (both are free to use). PEAR generates RPC-encoded XML as its output message, whereas NuSOAP can be made to generate document literal XML to be used with SOAP, making NuSOAP the better choice for working with the Google AdWords API web services.

PHP Resources

If you need some more background with PHP to understand the code in this chapter, there are a number of good resources available.

The PHP books on my shelf that I refer to most often are Programming PHP (Lerdorf and Tatroe) and PHP Cookbook (Sklar and Trachtenberg), both published by O'Reilly Media, Inc. The PHP language web site is You'll find a basic PHP tutorial on the site at, an online PHP manual at, and a catalog of online PHP references at

The Open Directory Project categorizes more than 1500 PHP resources at

NuSoap does not take care of all the details of using document literal SOAP. Specifically, you have to generate the XML in your code used to wrap elements that are used when NuSOAP generates an input message to one of the AdWords API web services.

14.1.2. Downloading and Installing NuSOAP

The NuSOAP toolkit consists of a primary file, nusoap.php, and 12 supporting files, all containing PHP code. You must have all these files in order to use NuSOAP. You can download a zipped archive containing the complete NuSOAP files, documentation, and samples from

Beware of the NuSoap download link provided in the Google AdWords API document, which will lead you to separately download each NuSOAP file, which is not necessarily a problem, but certainly a nuisance.

It's most common to write PHP code to a file on your local computer, using a text editor or an editor specifically intended for programming, and then upload the file to your web server and run it from that server.

Assuming this is the approach you're taking, follow these steps to get NuSOAP working with your PHP installation and development environment:

  1. Extract the contents of the NuSOAP zipped archive to your local computer.

  2. Use FTP to copy the PHP files in the NuSOAP lib directory from your local computer to a location on your web server where they will be accessible to your PHP programs. I created a folder named NuSOAP for these files immediately beneath the folder used for my PHP program files.

  3. Include the nusoap.php library file in each of your PHP code pages that will need to invoke the NuSOAP toolkit, using the PHP require_once( ) function. For example, if the NuSOAP files are located like mine in relationship to the program files:


Google Advertising Tools. Cashing in with AdSense, AdWords, and the Google APIs
Google Advertising Tools: Cashing in with Adsense, Adwords, and the Google APIs
ISBN: 0596101082
EAN: 2147483647
Year: 2004
Pages: 145
Authors: Harold Davis

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: