PHP get adsense earnings and reports

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.

Last Updated On : 9th November 2011

Subscribe to get updates delivered to your inbox

7 Comments + Add Comment

Leave a comment