PHP AdSense account monitor


/ Published in: PHP
Save to your folder(s)

PHP AdSense account monitor class, can retrieve data from your AdSense account (impressions, clicks, ctr, ecpm, earnings).
This class can retrieve (for now) only “AdSense for Content” data, for different periods of time (see class methods for more details). You can implement this PHP class in your own applications.
Note: this code is absolete. Check new project at http://code.google.com/p/php-adsense-account-library/


Copy this code and paste it in your HTML
  1. //---------------------------------------------------------------------------------------------------------------------
  2. //Source code for AdSense.php:
  3. //---------------------------------------------------------------------------------------------------------------------
  4.  
  5.  
  6.  
  7. /**
  8.  * PHP class ment to collect AdSense account data
  9.  * It can return various data for different periods of time
  10.  *
  11.  * @copyright Copyright © 2007
  12.  * @package AdSense
  13.  */
  14. class AdSense {
  15.  
  16.  
  17. /**
  18. * Stores curl connection handle
  19. *
  20. * @var resource
  21. */
  22. var $curl = null;
  23.  
  24.  
  25. /**
  26. * Stores TMP folder path
  27. * This folder must be writeble
  28. *
  29. * @var string
  30. */
  31. var $tmpPath = '/tmp';
  32.  
  33.  
  34. /**
  35. * AdSense::AdSense()
  36. * AdSense class constructor
  37. */
  38. function AdSense(){
  39. $this->coockieFile = tempnam($this->tmpPath, 'cookie');
  40.  
  41. $this->curl = curl_init();
  42. curl_setopt($this->curl, CURLOPT_HEADER, false);
  43. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
  45. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
  46. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
  47. 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");
  48. curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieFile);
  49. curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieFile);
  50.  
  51. register_shutdown_function(array(&$this, '__destructor'));
  52. }
  53.  
  54.  
  55. /**
  56. * AdSense::__destructor()
  57. * AdSense class destructor
  58. */
  59. function __destructor(){
  60. @curl_close($this->curl);
  61. @unlink($this->coockieFile);
  62. }
  63.  
  64.  
  65. /**
  66. * AdSense::connect()
  67. * Connects to AdSense account using supplied credentials
  68. * Returns true on unsuccessful connection, false otherwise
  69. *
  70. * @param string $username AdSense username
  71. * @param string $password AdSense password
  72. * @return boolean
  73. */
  74. function connect($username, $password){
  75. // phase 1
  76. 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");
  77. preg_match_all('<input type="hidden" name="(.*?)" value="(.*?)">', curl_exec($this->curl), $out);
  78. $params = array();
  79. foreach($out[1] as $key=>$name) { $params[] = $name . '=' . urlencode($out[2][$key]); }
  80. $params[] = 'Email=' . urlencode($username);
  81. $params[] = 'Passwd=' . urlencode($password);
  82. $params[] = 'null=' . urlencode('Sign in');
  83.  
  84. // phase 2
  85. curl_setopt($this->curl, CURLOPT_POST, true);
  86. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth");
  87. curl_setopt($this->curl, CURLOPT_POSTFIELDS, join('&', $params));
  88. preg_match("/.*<a target=\"_top\" href=\"(.*)\" style.*/", curl_exec($this->curl), $matches);
  89.  
  90. // phase 3
  91. curl_setopt($this->curl, CURLOPT_POST, false);
  92. curl_setopt($this->curl, CURLOPT_URL, $matches[1]);
  93.  
  94. // did we login ?
  95. if (eregi("Log out", curl_exec($this->curl))) {
  96. return true;
  97. } else {
  98. return false;
  99. };
  100. }
  101.  
  102.  
  103. /**
  104. * AdSense::parse()
  105. * Parses AdSense page and gets all stats
  106. * Returns associative array with collected data
  107. *
  108. * @param string $content AdSense page content
  109. * @return array
  110. */
  111. function parse($content){
  112. preg_match_all('/<td nowrap valign="top" style="text-align:right" class="">(.*?)<\/td>/', $content, $matches);
  113. return array(
  114. "impressions" => $matches[1][0],
  115. "clicks" => $matches[1][1],
  116. "ctr" => $matches[1][2],
  117. "ecpm" => $matches[1][3],
  118. "earnings" => $matches[1][4]
  119. );
  120.  
  121. }
  122.  
  123.  
  124. /**
  125. * AdSense::today()
  126. * Gets AdSense data for the period: today
  127. * Returns associative array with collected data
  128. *
  129. * @return array
  130. */
  131. function today(){
  132. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=today");
  133. return $this->parse(curl_exec($this->curl));
  134. }
  135.  
  136.  
  137. /**
  138. * AdSense::yesterday()
  139. * Gets AdSense data for the period: yesterday
  140. * Returns associative array with collected data
  141. *
  142. * @return array
  143. */
  144. function yesterday(){
  145. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=yesterday");
  146. return $this->parse(curl_exec($this->curl));
  147. }
  148.  
  149.  
  150. /**
  151. * AdSense::last7days()
  152. * Gets AdSense data for the period: last7days
  153. * Returns associative array with collected data
  154. *
  155. * @return array
  156. */
  157. function last7days(){
  158. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=last7days");
  159. return $this->parse(curl_exec($this->curl));
  160. }
  161.  
  162.  
  163. /**
  164. * AdSense::lastmonth()
  165. * Gets AdSense data for the period: lastmonth
  166. * Returns associative array with collected data
  167. *
  168. * @return array
  169. */
  170. function lastmonth(){
  171. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=lastmonth");
  172. return $this->parse(curl_exec($this->curl));
  173. }
  174.  
  175.  
  176. /**
  177. * AdSense::thismonth()
  178. * Gets AdSense data for the period: thismonth
  179. * Returns associative array with collected data
  180. *
  181. * @return array
  182. */
  183. function thismonth(){
  184. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=thismonth");
  185. return $this->parse(curl_exec($this->curl));
  186. }
  187.  
  188.  
  189. /**
  190. * AdSense::sincelastpayment()
  191. * Gets AdSense data for the period: sincelastpayment
  192. * Returns associative array with collected data
  193. *
  194. * @return array
  195. */
  196. function sincelastpayment(){
  197. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=sincelastpayment");
  198. return $this->parse(curl_exec($this->curl));
  199. }
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206. //---------------------------------------------------------------------------------------------------------------------
  207. //Source code for example.php
  208. //---------------------------------------------------------------------------------------------------------------------
  209.  
  210.  
  211.  
  212. include('AdSense.php');
  213.  
  214. $adsense = new AdSense();
  215. if ($adsense->connect('username', 'password')) {
  216. $data = $adsense->sincelastpayment();
  217. } else {
  218. die('Could not login to AdSense account.');
  219. };

URL: http://www.webtoolkit.info/php-adsense-account-monitor.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.