Google just announced an API for Adsense. Speaking from a somewhat selfish perspective, this seems like it would be a great candidate for a Drupal module, especially with Drupal's multi-user blogging capabilities.

Comments

incom’s picture

I am also interested to know whether there is any module for this

coreb’s picture

incom’s picture

Please note that AdSense module is only for the publisher to use adsense code in the site.

but AdSense API is to programmatically create and access AdSense accounts through web services. It provides a subset of the functionality available at the AdSense website.

This is intended for developers who want to programmatically create and access AdSense accounts.

incom’s picture

i want to implement AdSense API to drupal 4.76 to allow my users to apply for AdSense without leaving my site

To get started, just follow the instructions in the Getting Started Guide, available at http://code.google.com/apis/adsense/gettingstarted.html

the following are sample code in php and required NuSOAP Toolkit for PHP

1. common.php

<?php

// Copyright 2005, Google Inc. All rights reserved.

/**
 *  Show the error messages of a soap exception.
 *  Will also print the soap request and response. 
 */
function showMyErrors($client) {
  // print the soap
  showRequestResponse($client);
   
  // print the error information
  echo '<h2>Fault Code</h2>' . "\n";
  echo "<b>Fault:</b>  {$client->fault}<br>\n";
  echo "<b>Code:</b> {$client->faultcode}<br>\n";
  echo "<b>String:</b> {$client->faultstring}<br>\n";
  echo "<b>Detail:</b> {$client->faultdetail}<br>\n";
}

/**
 * Show bad construction error message.
 */
function showSoapClientError($err) {
  echo '<h2>Soap Client Error</h2>' . "\n";
  echo '<pre>' . $err . '</pre>';
  echo "\n";
}

/**
 * Show calling method.
 */
function showCall($methodName, $param) {
  echo '<h2>Method Called</h2>';
  echo "<b>method:</b> $methodName<br>";
  echo '<b>param:</b> ' . toHtml($param) . "<br>\n";
}

/**
 *  Print soap request and response
 */
function showRequestResponse($client) { 
  // print request
  echo '<h2>Request</h2>' . "\n";
  echo '<pre>' . toHtml($client->request) . '</pre>';
  echo "\n";
   
  // print response
  echo '<h2>Response</h2>'. "\n";
  echo '<pre>' . toHtml($client->response) . '</pre>';
  echo "\n";

  // Uncomment the following lines to print debug statements
  /*
  echo '<h2>Debug</h2>' . "\n";
  echo '<pre>' . toHtml($client->debug_str) . '</pre>';
  echo "\n";
  */
}

/**
 * Show the syn service id.
 */
function showSynServiceId($synServiceId) {
  echo '<h2>Service Id</h2>' . "\n";
  echo '<b>synServiceId:</b> ' . $synServiceId . "<br>\n";
}

/**
 * Print snippet
 */
function showSnippet($snippet) {
  echo '<h2>Snippet</h2>' . "\n";
  $text = toHtml($snippet, ENT_QUOTES);   
  echo '<pre>' . $text . '</pre>';
  echo "\n";
}

/**
 *  Print the result of account creation
 */
function showCreatedPublisher($response) {
  echo '<h2>Created Publisher</h2>' . "\n";
  echo "<b>id:</b> " . $response['id'] . "<br>\n"; 
  echo "<b>type:</b> " . $response['type'] . "<br>\n";
}

/**
 *  Get rid of the tag in html so it is displayable.
 */
function toHtml($text)
{
  $my_text = htmlspecialchars($text, ENT_QUOTES);
  $remove = array ("&lt;", " xmlns"); 
  $insert = array ("<br>&lt;", "<br>    xmlns"); 
  $my_text = str_replace($remove, $insert, $my_text);
  return $my_text;
}
?>

and

2. CreateAccount.php

<?php

// Copyright 2005, Google Inc. All rights reserved.

/** 
 *  sample code to create an Adsense account through Adsense API
 */

require_once('lib/nusoap.php');
require_once('common.php');

$server = 'https://www.google.com';
$namespace = 'http://www.google.com/api/adsense/v2';

// Set up the authentication headers
$email = '<impl:developer_email>REPLACE WITH DEVELOPER EMAIL</impl:developer_email>';
$password = '<impl:developer_password>REPLACE WITH DEVELOPER PASSWORD</impl:developer_password>';
$client_id = '<impl:client_id>NOT RELEVANT</impl:client_id>';

$header = $email . $password . $client_id;

// creating soap client
$wsdl = $server . '/api/adsense/v2/AccountService?wsdl';
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}
$client->soap_defencoding = 'UTF-8';

// Set the headers; they are needed for authentication
$client->setHeaders($header);
if ($client->fault) { 
  showMyErrors($client);
  return;
} 
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// setting the parameter
$param = '<loginEmail>users_address_here@example.com</loginEmail>';
$param .= '<entityType>Individual</entityType>';
$param .= '<websiteUrl>http://test.aaa.com</websiteUrl>';
$param .= '<websiteLocale>en</websiteLocale>';
$param .= '<usersPreferredLocale>en_US</usersPreferredLocale>';
$param .= '<emailPromotionPreferences>true</emailPromotionPreferences>';
$param .= '<synServiceTypes>ContentAds</synServiceTypes>';
$param .= '<hasAcceptedTCs>true</hasAcceptedTCs>';
$param = '<createAdSenseAccount>' . $param . '</createAdSenseAccount>';

// invoke web service
showCall('createAdSenseAccount', $param);
$response = $client->call('createAdSenseAccount', $param, $namespace);
if ($client->fault) { 
  showMyErrors($client);
  return;
} 
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// get back the response
$response = $response['createAdSenseAccountReturn'];
showCreatedPublisher($response);

// showing the soap
showRequestResponse($client);

?>

Please guide me in this regard

matojo2006’s picture

You can find some Google AdSense API sample code and also downlod a demo website here - http://www.dotnetspider.com/adsense/AdSenseAPISamples.aspx

incom’s picture

i have not seen any request or post regarding Adsense API module development in Drupal. Is this because AdSense API cannot use in Drupal.