Adsense Grabber --php-- snippet


SUBMITTED BY: TheSwarm

DATE: Oct. 20, 2015, 7:40 p.m.

FORMAT: Text only

SIZE: 6.4 kB

HITS: 2352

  1. <?php
  2. class AdSense {
  3. /**
  4. * Stores curl connection handle
  5. *
  6. * @var resource
  7. */
  8. var $curl = null;
  9. /**
  10. * Stores TMP folder path
  11. * This folder must be writeble
  12. *
  13. * @var string
  14. */
  15. var $tmpPath = '/tmp';
  16. /**
  17. * AdSense::AdSense()
  18. * AdSense class constructor
  19. */
  20. function AdSense(){
  21. $this->coockieFile = tempnam($this->tmpPath, 'cookie');
  22. $this->curl = curl_init();
  23. curl_setopt($this->curl, CURLOPT_HEADER, false);
  24. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
  26. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
  27. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
  28. 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");
  29. curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieFile);
  30. curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieFile);
  31. register_shutdown_function(array(&$this, '__destructor'));
  32. }
  33. /**
  34. * AdSense::__destructor()
  35. * AdSense class destructor
  36. */
  37. function __destructor(){
  38. @curl_close($this->curl);
  39. @unlink($this->coockieFile);
  40. }
  41. /**
  42. * AdSense::connect()
  43. * Connects to AdSense account using supplied credentials
  44. * Returns true on unsuccessful connection, false otherwise
  45. *
  46. * @param string $username AdSense username
  47. * @param string $password AdSense password
  48. * @return boolean
  49. */
  50. function connect($username, $password){
  51. // phase 1
  52. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth?service=adsense&hl=en-US&ltmpl=login&ifr=
  53. true&passive=true&rm=hide&nui=3&alwf=true&continue=https%3A%2F%2Fwww.google.com%2Fadsense%
  54. 2Fgaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth");
  55. preg_match_all('<input type="hidden" name="(.*?)" value="(.*?)">', curl_exec($this->curl), $out);
  56. $params = array();
  57. foreach($out[1] as $key=>$name) { $params[] = $name . '=' . urlencode($out[2][$key]); }
  58. $params[] = 'Email=' . urlencode($username);
  59. $params[] = 'Passwd=' . urlencode($password);
  60. $params[] = 'null=' . urlencode('Sign in');
  61. // phase 2
  62. curl_setopt($this->curl, CURLOPT_POST, true);
  63. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth");
  64. curl_setopt($this->curl, CURLOPT_POSTFIELDS, join('&', $params));
  65. preg_match("/.*<a target=\"_top\" href=\"(.*)\" style.*/", curl_exec($this->curl), $matches);
  66. // phase 3
  67. curl_setopt($this->curl, CURLOPT_POST, false);
  68. curl_setopt($this->curl, CURLOPT_URL, $matches[1]);
  69. // did we login ?
  70. if (eregi("Log out", curl_exec($this->curl))) {
  71. return true;
  72. } else {
  73. return false;
  74. };
  75. }
  76. /**
  77. * AdSense::parse()
  78. * Parses AdSense page and gets all stats
  79. * Returns associative array with collected data
  80. *
  81. * @param string $content AdSense page content
  82. * @return array
  83. */
  84. function parse($content){
  85. preg_match_all('/<td nowrap valign="top" style="text-align:right" class="">(.*?)<\/td>/', $content, $matches);
  86. return array(
  87. "impressions" => $matches[1][0],
  88. "clicks" => $matches[1][1],
  89. "ctr" => $matches[1][2],
  90. "ecpm" => $matches[1][3],
  91. "earnings" => $matches[1][4]
  92. );
  93. }
  94. /**
  95. * AdSense::today()
  96. * Gets AdSense data for the period: today
  97. * Returns associative array with collected data
  98. *
  99. * @return array
  100. */
  101. function today(){
  102. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=today");
  103. return $this->parse(curl_exec($this->curl));
  104. }
  105. /**
  106. * AdSense::yesterday()
  107. * Gets AdSense data for the period: yesterday
  108. * Returns associative array with collected data
  109. *
  110. * @return array
  111. */
  112. function yesterday(){
  113. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=yesterday");
  114. return $this->parse(curl_exec($this->curl));
  115. }
  116. /**
  117. * AdSense::last7days()
  118. * Gets AdSense data for the period: last7days
  119. * Returns associative array with collected data
  120. *
  121. * @return array
  122. */
  123. function last7days(){
  124. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=last7days");
  125. return $this->parse(curl_exec($this->curl));
  126. }
  127. /**
  128. * AdSense::lastmonth()
  129. * Gets AdSense data for the period: lastmonth
  130. * Returns associative array with collected data
  131. *
  132. * @return array
  133. */
  134. function lastmonth(){
  135. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=lastmonth");
  136. return $this->parse(curl_exec($this->curl));
  137. }
  138. /**
  139. * AdSense::thismonth()
  140. * Gets AdSense data for the period: thismonth
  141. * Returns associative array with collected data
  142. *
  143. * @return array
  144. */
  145. function thismonth(){
  146. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=thismonth");
  147. return $this->parse(curl_exec($this->curl));
  148. }
  149. /**
  150. * AdSense::sincelastpayment()
  151. * Gets AdSense data for the period: sincelastpayment
  152. * Returns associative array with collected data
  153. *
  154. * @return array
  155. */
  156. function sincelastpayment(){
  157. curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=sincelastpayment");
  158. return $this->parse(curl_exec($this->curl));
  159. }
  160. }
  161. ?>
  162. Source code for example.php
  163. <?php
  164. include('AdSense.php');
  165. $adsense = new AdSense();
  166. if ($adsense->connect('username', 'password')) {
  167. $data = $adsense->sincelastpayment();
  168. } else {
  169. die('Could not login to AdSense account.');
  170. };
  171. ?>

comments powered by Disqus