PHP get adsense earnings and reports

By | November 9, 2011

In this article we are going to login into Google Adsense and retrieve earnings. Google Adsense now has a new interface and so needs a different kind of coding.

In this example we shall fetch "yesterday's" earnings sitewise.

Create a Report to be fetched :

First open the sitewise report in the Performance Tab in the new interface of Google Adsense at www.adsense.com and save the report with a name say "tomato". The php script will fetch this particular report. The new adsense interface is highly ajax based and so it is not easy or feasible to open it directly. Instead this report will be opened in "Low Bandwidth" or "Html" or "Mobile" mode.

Here is an example url :

https://www.google.com/adsense/v3/m/reports?savedreport=tomato

Such a url can also be opened from a php script/bot very easily and data be read. This is the basic trick that enables fetching the report very effectively from a php script.

Write the Code :

The basic idea is to login using this login page :

Login

Here is a simple class that can login to Google Adsense :

class Adsense 
{
	private $user;
	private $pass;
	private $cookie;
	
	public function __construct ( $username , $password ) 
	{
		$this->username = $username;
		$this->password = $password;
		$this->cookie_file = tempnam("", 'adsense_cookie_file_');
		$this->cookie_jar = tempnam("", 'adsense_cookie_jar_');
		
		fclose(fopen($this->cookie_file , 'w+'));
		chmod($this->cookie_file , 0777);
		
		fclose(fopen($this->cookie_jar , 'w+'));
		chmod($this->cookie_jar , 0777);
		
		$this->_setup_curl();
		
		$this->_login();
	}
	
	private function _setup_curl()
	{
		$curl = curl_init();
		
		curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, true );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
		
		//cookie files
		curl_setopt ( $curl, CURLOPT_COOKIEJAR, $this->cookie_jar );
		curl_setopt ( $curl, CURLOPT_COOKIEFILE, $this->cookie_file );
		
		curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 0);
		
		$this->curl = $curl;
	}
	
	function _getcurl ( $url, $post_data = false ) 
	{
		$curl = $this->curl;
		
		curl_setopt($curl, CURLOPT_URL, $url);

		if ( is_array($post_data) ) 
		{
			//echo http_build_query($post_data);
			curl_setopt ( $curl , CURLOPT_POST , 1 );
			curl_setopt ( $curl , CURLOPT_POSTFIELDS , http_build_query($post_data) );
		}
		else
		{
			curl_setopt ( $curl , CURLOPT_POST , 0 );
		}

		$return_data = curl_exec ( $curl );
		//echo $return_data;
		
		return $return_data;
	}

	private function _login ( ) 
	{
		echo "Fetching login page";
		
		$data = $this -> _getcurl ( "https://accounts.google.com/ServiceLoginBox?service=adsense&ltmpl=login&ifr=true&rm=hide&fpui=3&nui=15&alwf=true&passive=true&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&hl=en_US" );
		
		$input_tags = get_input_tags($data);
		
		//Login data to be submitted
		$post_data = array(
			'continue' => 'https://www.google.com/adsense/login-box-gaiaauth' , 
			'followup' => 'https://www.google.com/adsense/login-box-gaiaauth' , 
			'service' => 'adsense' ,
			'nui' => '15' , 
			'ifr' => 'true' , 
			'rm' => 'hide' , 
			'dsh' => $input_tags['dsh'] , 
			'ltmpl' => 'login' , 
			'hl' => 'en_US' , 
			'alwf' => 'true' , 
			'GALX' => $input_tags['GALX'] ,
			'Email'=> $this->username , 
			'Passwd' => $this->password
		);
		
		echo 'Submitting login page';
		$data = $this->_getcurl('https://accounts.google.com/ServiceLoginBoxAuth' , $post_data);
		
		if ( strpos ( $data, 'Invalid username or password.' ) ) 
		{
			unlink ( $this -> cookie );
			die("Login failed.n");
		}
		
		echo 'Checkcookie thing';
		$data = $this -> _getcurl ( "https://accounts.google.com/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&hl=en_US&service=adsense&ltmpl=login&chtml=LoginDoneHtml" );
	}
}
	
function get_input_tags($html)
{
	$post_data = array();
	
	// a new dom object
    $dom = new domDocument; 
	
	// load the html into the object
    $dom->loadHTML($html); 
    // discard white space
    $dom->preserveWhiteSpace = false; 
    
    // all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 

    // get all rows from the table
	for ($i = 0; $i < $input_tags->length; $i++) 
    {
  		if( is_object($input_tags->item($i)) )
		{
			$name = $value = '';
			$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
			if(is_object($name_o))
			{
				$name = $name_o->value;
				
				$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
				if(is_object($value_o))
				{
					$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
				}
				
				$post_data[$name] = $value;
			}
		}
	}
	
	return $post_data;
}

Most of it is CURL except the Google post data. All that the above class accomplish is logging into Google Adsense. It does nothing more.

The method get_input_tags is a generic method that creates an array of name => value of all input tags in an html page.

Next comes the actual fetching of the report "tomato" , which is again quite simple :

require_once('adsense.class.php' );

$username = 'your_username';
$password = 'your_password';

//new instance
$adsense = new Adsense ( $username , $password );

$report = 'tomato';

//get the report from mobile interface :)  , smart boy!!
$html = $adsense->_getcurl("https://www.google.com/adsense/v3/m/reports?savedreport=$report");

So Simple!! We now have the Google Adsense report for "yesterdays" earnings and it has all the data we need.

Parse the results :

All that is left now is to parse the results which can be done using DomDoc like this :

//now parse the ytable
$dom = new domDocument; 
$dom->loadHTML($html); 
// discard white space
$dom->preserveWhiteSpace = false; 

$div = $dom->getElementById('table');

if($div == NULL)
{
	echo "Nothing found for id table";
}

$sites = array();

$table = $div->getElementsByTagName('table')->item(0);

if(is_object($table) and $table != NULL)
{
	$tbody = $table->getElementsByTagName('tbody')->item(0);
	$rows = $tbody->getElementsByTagName('tr');
	
	$c = 0;
	
	// get all rows from the table
	for ($i = 0; $i < $rows->length; $i++) 
	{
		$row = $rows->item($i);
		
		$cols = $row->getElementsByTagName('td');
	
		//Site name
		$r['site'] = trim( str_replace('<br>' , '' , strip_tags(trim($cols->item(0)->nodeValue ) )) );
		
		//Page views
		$r['page_views'] = floatval(trim(str_replace( ',' , '' , $cols->item(1)->nodeValue )));
		
		//Clicks
		$r['clicks'] = floatval(trim( $cols->item(2)->nodeValue ));
		
		//Page CTR
		$r['page_ctr'] = floatval(trim(str_replace( '%' , '' , $cols->item(3)->nodeValue )));
		
		//CPC
		$r['cpc'] = floatval(trim(str_replace( '$' , '' , $cols->item(4)->nodeValue )));
		
		//Page RPM
		$r['page_rpm'] = floatval(trim(str_replace( '$' , '' , $cols->item(5)->nodeValue )));
		
		//Estimated Earnings
		$r['estimated_earnings'] = floatval(trim(str_replace( '$' , '' , $cols->item(6)->nodeValue )));
		
		//Get yesterday's date
		$r['date'] = date('Y-m-d' , strtotime('-1 days'));
		
		$sites[] = $r;
		
		$c++;
	}
}
echo "<pre>";
print_r($sites);
echo "</pre>";

And we are done. The $sites array has the report as an array.

Conclusion :

Google has now launched a new api called the Google Adsense Management API that can be used in a more "official" way to perform the same task that we did above. Check out http://code.google.com/apis/adsense/management/index.html.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

7 Comments

PHP get adsense earnings and reports
  1. Jason Fox

    Your script is EXACTLY what we needed. The ‘official’ adsense management API truly sucks compared to your simple script. Let’s hope they don’t change the report format anytime soon! Thanks again for your code!

  2. Max

    Great stuff! I have been looking for an easy-to-use script for adsense for quite a while. It’s so much better than the one deployed by google: Too much overload!

  3. Kim

    Thank you very much for your answer, but I already have tried to login using:

    1. get https://accounts.google.com/ServiceLogin?service=lbc

    2. post to https://accounts.google.com/ServiceLoginAuth with the following post_data:

    $post_data = array(
    ‘service’ => ‘lbc’ ,
    ‘dsh’ => $input_tags[‘dsh’] ,
    ‘GALX’ => $input_tags[‘GALX’] ,
    ‘pstMsg’ => ‘1’ ,
    ‘dnConn’ => ‘https%3A%2F%2Faccounts.youtube.com’ ,
    ‘timeStmp’ => ” ,
    ‘secTok’ => ” ,
    ‘Email’=> $this->username ,
    ‘Passwd’ => $this->password
    );

    3. Checkcookie thing https://accounts.google.com/CheckCookie?service=lbc&chtml=LoginDoneHtml&dnConn=https%3A%2F%2Faccounts.youtube.com&pstMsg=1

    But I keep getting the following html:

    [script type=”text/javascript” language=”javascript”]
    // Accessing window.external members can cause IE to throw exceptions.
    // Any code that acesses window.external members must be try/catch wrapped
    /** @preserveTry */
    try {
    if (top == self) {
    if (window.gtbExternal) {
    window.gtbExternal.setM();
    } else {
    window.external.setM();
    }
    }
    }
    catch(err) {
    }
    [/script[
    [meta http-equiv=”refresh” content=”0; url='http://www.google.dk/accounts/SetSID?ssdc=1&sidt=ALWU2ct1rO4S%2FKuWD%2FTGorhebkepjo3mTSHuYElfcSpr9HLtLaoeV4RuqxA6PRTrrqphj%2BHy8iIWpKqUswRAQbV76HlDI2ygNncs4OLyvuecqLE%2FU0d4RhzQIc8rl%2BdZzd0K8figZ3VYePflHQgNYCEYGZ%2F8mJ94r7DcaT%2BhguiBKpWW%2BEh6XOCRIcVxct9Cdf7AUOLPdXT5b86Fv7kGhFkIz9vyqDOHUh%2F%2BPyt9O9smNb%2BgwSFMcorRXZ9dKCn5NfN%2BgVgzd2Zr%2FWW1idpX3w3eXGzPZEGlkw%3D%3D&continue=https%3A%2F%2Faccounts.google.com%2FServiceLogin%3Fpassive%3Dtrue%26go%3Dtrue%26service%3Dlbc%26fss%3D1%26continue%3Dhttps%253A%252F%252Faccounts.google.com%252FManageAccount'”][/head]
    [body bgcolor=”#ffffff” text=”#000000″ link=”#0000cc” vlink=”#551a8b” alink=”#ff0000″][script type=”text/javascript” language=”javascript”]
    location.replace(“http://www.google.dk/accounts/SetSID?ssdcx3d1x26sidtx3dALWU2ct1rO4S%2FKuWD%2FTGorhebkepjo3mTSHuYElfcSpr9HLtLaoeV4RuqxA6PRTrrqphj%2BHy8iIWpKqUswRAQbV76HlDI2ygNncs4OLyvuecqLE%2FU0d4RhzQIc8rl%2BdZzd0K8figZ3VYePflHQgNYCEYGZ%2F8mJ94r7DcaT%2BhguiBKpWW%2BEh6XOCRIcVxct9Cdf7AUOLPdXT5b86Fv7kGhFkIz9vyqDOHUh%2F%2BPyt9O9smNb%2BgwSFMcorRXZ9dKCn5NfN%2BgVgzd2Zr%2FWW1idpX3w3eXGzPZEGlkw%3D%3Dx26continuex3dhttps%3A%2F%2Faccounts.google.com%2FServiceLogin%3Fpassive%3Dtrue%26go%3Dtrue%26service%3Dlbc%26fss%3D1%26continue%3Dhttps%253A%252F%252Faccounts.google.com%252FManageAccount”)
    [/script][/body][/html]

  4. Kim

    Hi Binary tides,

    Thank you for the script. It works like a charm! I have a little problem, which I hope you can help me with:

    I’ve tried to use your script to login to Google Local Business Center by changing line 12 in the report fetching part. I thought this would work since I can access the Google LBC page if I first log in to adsense in a normal browsing session:

    From:
    //get the report from mobile interface :) , smart boy!!
    $html = $adsense->_getcurl(“https://www.google.com/adsense/v3/m/reports?savedreport=$report”);

    To:
    //get the report from mobile interface :) , smart boy!!
    $html = $adsense->_getcurl(“http://www.google.com/local/add/businessCenter?pli=1”);

    This doesn’t work and I get a simple html page, that meta redirects to a different page. I’ve tried to parse the page and fetch the meta redirect, but I keep getting redirected to the same page. Can you please help me out? :)

Leave a Reply

Your email address will not be published. Required fields are marked *