Thursday, June 30, 2011 | 8:54 AM
This is the third and final post in the “AdSense API from Scratch” series, where we take a look at how to develop a full AdSense API implementation, using best practices, in PHP 5.
In Part 1, we looked at how to get started with development, and made our first createAccount call. In Part 2, we dealt with error handling and account associations. This time, we’ll look at how to generate ad code, and the best practices to follow when doing so.
Generating ad code
For the first time in this guide, we’re accessing a service other than the AccountService, namely the AdSenseForContentService, so we need to provide a different WSDL location. Note that this also applies to the AdSenseForSearchService, but we’re using AdSense for Content (AFC) as an example.
$wsdl = 'https://sandbox.google.com/api/adsense/v3/' +
// Get AdSenseForContentService.
$afcService = new SoapClient($wsdl, $options);
We’re also performing an operation on a publisher account, instead of simply creating or associating with one. We thus need to specify which account to work with, via the
$client_id = 'ca-pub-xxxxxxxxxxxxxxxx';
$client_id_header = new SoapHeader($namespace, 'client_id', $client_id);
$developer_password_header, $display_locale_header, $client_id_header));
The client ID is more commonly known as the publisher ID, and in the case of AFC, is of the form
After this, it’s a matter of passing in our ad unit preferences:
$result = $afcService->generateAdCode(array(
'synServiceId' => 'ca-pub-xxxxxxxxxxxxxxxx',
'adStyle' => array(
'backgroundColor' => '#FFFFFF',
'borderColor' => '#000000',
'textColor' => '#00FF00',
'titleColor' => '#0000CC',
'urlColor' => '#FF3300'),
'adUnitType' => array('value' => 'TextOnly'),
'adLayout' => array('value' => '728x90'),
'isFramedPage' => false,
'cornerStyles' => array(
'value' => 'DEFAULT')));
You can find the full example for this method, including basic error handling, here.
createAccount and generateAdCode
An important step of setting up a new account is generating ad code for it. This is because the generateAdCode method has the side effect of initializing certain features in a new account, particularly when it comes to reporting.
Because of this, we recommend that you run
generateAdCode right after createAccount, if possible; you don’t need to wait for the publisher account to be verified and approved. Feel free to discard the generated HTML, as we’re only interested in the side effects!
If your implementation automatically inserts the ad code into your publishers’ pages, there is one other fact that you need to be aware of, when it comes to ad code generation: ad code generation is static, and the resulting code is tied to the specified publisher account.
This means that if your system gives users the ability to create a new AdSense account (or associate to an existing one) after the initial one, you’ll need to make sure that you regenerate and update all of their ad code, so that ads will be tied to the new account. This is a common mistake many implementations make and something we actively look for in the review process, so save time by thinking about it in advance!
We’ve gone from an empty text editor to three full examples of how to perform the basic AdSense API host functionality: account creation, account association and ad code generation. I hope you found this series informative, and that it sets you well on your way to coding your own implementation!
For any questions, comments or suggestions, feel free to talk to us in the forum!