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/
Source code for AdSense.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<?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/index.html';
/**
* 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<mpl=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));
}
}
?>
|
Source code for example.php
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
include('/AdSense.html');
$adsense = new AdSense();
if ($adsense->connect('username', 'password')) {
$data = $adsense->sincelastpayment();
} else {
die('Could not login to AdSense account.');
};
?>
|