Location: PHPKode > scripts > PHP AdSense account monitor > PHP-AdSense-account.php
<?php
 
/**
 * PHP class ment to collect AdSense account data
 * It can return various data for different periods of time
 *
 * @copyright	Copyright © 2007
 * @package		AdSense
 */
class AdSense {
 
 
	/**
	 * Stores curl connection handle
	 *
	 * @var resource
	 */
	var $curl = null;
 
 
	/**
	 * Stores TMP folder path
	 * This folder must be writeble
	 *
	 * @var string
	 */
	var $tmpPath = '/tmp';
 
 
	/**
	 * AdSense::AdSense()
	 * AdSense class constructor
	 */
	function AdSense(){
		$this->coockieFile = tempnam($this->tmpPath, 'cookie');
 
		$this->curl = curl_init();
		curl_setopt($this->curl, CURLOPT_HEADER, false);
		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
		curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieFile);
		curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieFile);
 
		register_shutdown_function(array(&$this, '__destructor'));
	}
 
 
	/**
	 * AdSense::__destructor()
	 * AdSense class destructor
	 */
	function __destructor(){
		@curl_close($this->curl);
		@unlink($this->coockieFile);
	}
 
 
	/**
	 * AdSense::connect()
	 * Connects to AdSense account using supplied credentials
	 * Returns true on unsuccessful connection, false otherwise
	 *
	 * @param string $username AdSense username
	 * @param string $password AdSense password
	 * @return boolean
	 */
	function connect($username, $password){
		// phase 1
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth?service=adsense&hl=en-US&ltmpl=login&ifr=true&passive=true&rm=hide&nui=3&alwf=true&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth");
		preg_match_all('<input type="hidden" name="(.*?)" value="(.*?)">', curl_exec($this->curl), $out);
		$params = array();
		foreach($out[1] as $key=>$name) { $params[] = $name . '=' . urlencode($out[2][$key]); }
		$params[] = 'Email=' . urlencode($username);
		$params[] = 'Passwd=' . urlencode($password);
		$params[] = 'null=' . urlencode('Sign in');
 
		// phase 2
		curl_setopt($this->curl, CURLOPT_POST, true);
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth");
		curl_setopt($this->curl, CURLOPT_POSTFIELDS, join('&', $params));
		preg_match("/.*<a target=\"_top\" href=\"(.*)\" style.*/", curl_exec($this->curl), $matches);
 
		// phase 3
		curl_setopt($this->curl, CURLOPT_POST, false);
		curl_setopt($this->curl, CURLOPT_URL, $matches[1]);
 
		// did we login ?
		if (eregi("Log out",  curl_exec($this->curl))) {
			return true;
		} else {
			return false;
		};
	}
 
 
	/**
	 * AdSense::parse()
	 * Parses AdSense page and gets all stats
	 * Returns associative array with collected data
	 *
	 * @param string $content AdSense page content
	 * @return array
	 */
	function parse($content){
		preg_match_all('/<td nowrap valign="top" style="text-align:right" class="">(.*?)<\/td>/', $content, $matches);
		return array(
			"impressions" => $matches[1][0],
			"clicks" => $matches[1][1],
			"ctr" => $matches[1][2],
			"ecpm" => $matches[1][3],
			"earnings" => $matches[1][4]
		);
 
	}
 
 
	/**
	 * AdSense::today()
	 * Gets AdSense data for the period: today
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function today(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=today");
		return $this->parse(curl_exec($this->curl));
	}
 
 
	/**
	 * AdSense::yesterday()
	 * Gets AdSense data for the period: yesterday
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function yesterday(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=yesterday");
		return $this->parse(curl_exec($this->curl));
	}
 
 
	/**
	 * AdSense::last7days()
	 * Gets AdSense data for the period: last7days
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function last7days(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=last7days");
		return $this->parse(curl_exec($this->curl));
	}
 
 
	/**
	 * AdSense::lastmonth()
	 * Gets AdSense data for the period: lastmonth
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function lastmonth(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=lastmonth");
		return $this->parse(curl_exec($this->curl));
	}
 
 
	/**
	 * AdSense::thismonth()
	 * Gets AdSense data for the period: thismonth
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function thismonth(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=thismonth");
		return $this->parse(curl_exec($this->curl));
	}
 
 
	/**
	 * AdSense::sincelastpayment()
	 * Gets AdSense data for the period: sincelastpayment
	 * Returns associative array with collected data
	 *
	 * @return array
	 */
	function sincelastpayment(){
		curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=sincelastpayment");
		return $this->parse(curl_exec($this->curl));
	}
 
}
 
?>
Return current item: PHP AdSense account monitor