Sample Code

Google provides the following sample code for clients that use the AdSense API. This code is provided for educational purposes only. Although we have tested all sample code, we cannot provide guarantees or support for it.

The AdSense API is a standard document/literal web service that we have tested with SOAP toolkits in:

The examples presented here are simple clients that show the basics of calling the AdSense API.

Java

We recommend using Apache Axis 1.2RC1 for building Java clients.

Installation

You can download Axis from archive.apache.org

Java Sample Code

Perl

We use Perl SOAP::Lite 0.60 for testing and have had good interoperability results with our service.

Installation

  1. Download Perl SOAP::Lite
  2. Gunzip and untar the downloaded file
  3. cd to SOAP-LITE-0.60
  4. perl Makefile.PL PREFIX=yourtargetdirectory
  5. make install

Using the Perl Library

   use lib yourtargetdirectory;
   use SOAP::Lite;
   # ...
   my $accountService = SOAP::Lite->service($accountWsdlUrl);
   # ...
NOTE - SOAP::Lite is not document/literal aware and tends to send things with SOAP encoding. The AdSense API server is modestly successful in understanding these requests.

Perl Sample Code

PHP

We recommend either of two methods for using PHP with the AdSense API — either download and use the SensAPIlity library or download the NuSOAP Toolkit and work with the SOAP requests directly as demonstrated in the sample code here.

Installation of SensAPIlity

  1. Go to Google SensAPIlity and follow the instructions.
NOTE: SensAPIlity is pronounced "sense-a-pil'-ity".

Installation of NuSOAP Toolkit for PHP

This toolkit is located at NuSOAP Toolkit for PHP.
  1. Download NuSOAP 0.7.2
  2. Unzip the bundle — the PHP library is in lib/nusoap.php
Important Note: When using NuSOAP with createAdSenseAccount and associateExistingAccount methods in AccountService, the return format may be unexpected. The response when the user is signed up for one syndication service differs from the response when the user is signed up for more than one syndication service. If a user is signed up for only one service, the user's service ID can be found at::
$response['associateExistingAccountReturn']['id']
while a user signed up for multiple services can have their ID's found at:
$response['associateExistingAccountReturn'][n]['id']
where n ranges from 0 to the number of services the user is signed up for. For addition information see post 1 and post 2 on the AdSense API Forum. SensAPIlity does not have this inconsistency.

Using PHP with the NuSOAP Library

   require_once('lib/nusoap.php');
   // ...
   $client = new soapclient($wsdl, 'wsdl');
   // ...

PHP Sample Code

Python

We recommend fpconst and SOAPpy.

Installation of fpconst

  1. Download fpconst
  2. gunzip and untar the downloaded file
  3. python setup.py install --prefix somelocation
  4. Add the directory that contains fpconst.py to $PYTHONPATH in .bashrc

Installation of SOAPpy

  1. Download SOAPpy
  2. gunzip and untar the downloaded file
  3. python setup.py install --prefix anotherlocation
  4. Add the directory that contains SOAPpy to $PYTHONPATH in .bashrc

Using the Python Library

   import SOAPpy
   # ...
   service = SOAPpy.SOAPProxy(wsdlUrl , header=headers)
   # ...

Python Sample Code

C#

We have tested with Microsoft Visual Studio. No library is needed.

Using Visual Studio

In Microsoft Visual Studio, right-click on "References" in the Solution Explorer window and select "Add Web Reference". Enter the URL of the WSDL file you want to add to your project, then click OK. You can then make calls to the web service as you would execute normal function calls.

C# Sample Code