Location: PHPKode > scripts > Retrieve Adsense Earnings > adsense.class.php
<?php
	/* ============================================= */
	/*         Adsense Earnings Retrieval            */
	/*     Class by Bruno Miguel - bplugins.com      */
	/*          This is free software :)             */
	/* ============================================= */

	class Adsense {
		var $user;
		var $pass;
		var $cookie;
		
		public function __construct ( $username, $password ) {
			$this -> user = $username;
			$this -> pass = $password;
			$this -> cookie = tempnam("", "adsense_");
			$this -> _login ();
		}

		private function _getcurl ( $url, $post_data = "" ) {

			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);

			if ( $post_data != "" ) {
				curl_setopt ( $curl, CURLOPT_POST, 1 );
				curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post_data );
			}

			curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, true );
			curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $curl, CURLOPT_COOKIEJAR, $this -> cookie );
			curl_setopt ( $curl, CURLOPT_COOKIEFILE, $this -> cookie );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);
			$return_data = curl_exec ( $curl );
			curl_close ( $curl );

			return $return_data;
		}

		private function _login ( ) {

			$data = $this -> _getcurl ( "https://www.google.com/accounts/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" );
			preg_match("/<input type=\"hidden\" name=\"GA3T\" value=\"(.*?)\"/", $data, $ga3t);
			preg_match("/<input type=\"hidden\" name=\"GALX\" value=\"(.*?)\"/", $data, $galx);
			$data = $this -> _getcurl ( "https://www.google.com/accounts/ServiceLoginBoxAuth", "continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&service=adsense&nui=15&fpui=3&ifr=true&rm=hide&ltmpl=login&hl=en_US&alwf=true&ltmpl=login&GA3T=$ga3t[1]&GALX=$galx[1]&Email=" . $this -> user ."&Passwd=" . $this -> pass );

			if ( strpos ( $data, 'Invalid username or password.' ) ) {
				unlink ( $this -> cookie );
				die("Login failed.\n");
			}

			$data = $this -> _getcurl ( "https://www.google.com/accounts/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" );
		}
		
		public function retrieve ( $time ) {
			$data = $this -> _getcurl ( "https://www.google.com/adsense/report/overview?timePeriod=" . $time );
			preg_match_all("/<td nowrap valign=\"top\" style=\"text-align\:right\" class=\"\">(.*?)<\/td>/", $data, $result);
			
			$return_data['impressions'] = $result[1][0];
			$return_data['clicks'] = $result[1][1];
			$return_data['ctr'] = $result[1][2];
			$return_data['ecpm'] = $result[1][3];
			$return_data['earnings'] = $result[1][4];
			
			return $return_data;
		}

		public function destroy ( ) {
			unlink ( $this -> cookie );
		}

	}
?>
Return current item: Retrieve Adsense Earnings