This post confirms my ownership of the site and that this site adheres to Google AdSense program policies and Terms and Conditions

Wednesday, March 10, 2010

Generate Ad Code using PHP




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



/**

* sample code to generate ad code snippet of an existing account through Adsense API

*/



require_once('lib/nusoap.php');

require_once('common.php');



$server = 'https://sandbox.google.com';

// Comment out the line above and uncomment the line below to use

// the production API server

// $server = 'https://www.google.com';

$namespace = 'http://www.google.com/api/adsense/v3';



// Set up the authentication headers

$email = "REPLACE WITH DEVELOPER EMAIL";

$password = "REPLACE WITH DEVELOPER PASSWORD";

$client_id = "REPLACE WITH CLIENT ID";



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



// creating soap client

$wsdl = $server . '/api/adsense/v3/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 = "ContentAds";



// invoke web service

showCall('getSyndicationService', $param);

$response = $client->call('getSyndicationService', $param, $namespace);

if ($client->fault) {

showMyErrors($client);

return;

}

$err = $client->getError();

if ($err) {

showSoapClientError($err);

return;

}



// get back the response

$response = $response['return'];

$synServiceId = $response['id'];

showSynServiceId($synServiceId);







// showing the soap

showRequestResponse($client);





// setup client soap for another webservice

$wsdl = $server . '/api/adsense/v3/AdSenseForContentService?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 up adstyle parameter

$ads_param = '';

$ads_param .= "demoStyle";

$ads_param .= '#0000FF';

$ads_param .= '#FFFFFF';

$ads_param .= '#FF0000';

$ads_param .= '#00FF00';

$ads_param .= '#FFFF00';

$ads_param .= '
';



// setting up parameter

$param = ''.$synServiceId.'';

$param .= $ads_param;

$param .= 'TextOnly';

$param .= '728x90';

$param .= '#FFFFFF';

$param .= 'false';

$param .= '';

$param .= 'DEFAULT';



$param = "" . $param . "";



// invoke the web service

showCall('generateAdCode', $param);

$response = $client->call('generateAdCode', $param, $namespace);

if ($client->fault) {

showMyErrors($client);

return;

}

$err = $client->getError();

if ($err) {

showSoapClientError($err);

return;

}



$response = $response['return'];

$snippet = $response;

showSnippet($snippet);



// showing the soap

showRequestResponse($client);



?>



source: http://code.google.com/p/google-sensapility/source/browse/trunk/samplecode/v3/GenerateAdCode.php

No comments:

Post a Comment