.01
.02
.03
.04
.05
.06
.07
.08
.09
.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
.200
.201
.202
.203
.204
.205
.206
.207
.208
.209
.210
.211
.212
.213
.214
.215
.216
.217
.218
.219
.220
.221
.222
.223
.224
.225
.226
.227
.228
.229
.230
.231
.232
.233
.234
.235
.236
.237
.238
.239
.240
.241
.242
.243
.244
.245
.246
.247
.248
.249
.250
.251
.252
.253
.254
.255
.256
.257
.258
.259
.260
.261
.262
.263
.264
.265
.266
.267
.268
.269
.270
.271
.272
.273
.274
.275
.276
.277
.278
.279
.280
.281
.282
.283
.284
.285
.286
.287
.288
.289
.290
.291
.292
.293
.294
.295
.296
.297
.298
.299
.300
.301
.302
.303
.304
.305
.306
.307
.308
.309
.310
.311
.312
.313
.314
.315
.316
.317
.318
.319
.320
.321
.322
.323
.324
.325
.326
.327
.328
.329
.330
.331
.332
.333
.334
.335
.336
.337
.338
.339
.340
.341
.342
.343
.344
.345
.346
.347
.348
.349
.350
.351
.352
.353
.354
.355
.356
.357
.358
.359
.360
.361
.362
.363
.364
.365
.366
.367
.368
.369
.370
.371
.372
.373
.374
.375
.376
.377
.378
.379
.380
.381
.382
.383
.384
.385
.386
.387
.388
.389
.390
.391
.392
.393
.394
.395
.396
.397
.398
.399
.400
.401
.402
.403
.404
.405
.406
.407
.408
.409
.410
.411
.412
.413
.414
.415
.416
.417
.418
.419
.420
.421
.422
.423
.424
.425
.426
.427
.428
|
|
<?php
/******************************************************************************/
/* */
/* __ ____ */
/* ___ / / ___ / __/__ __ _____________ ___ */
/* / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-< */
/* / .__/_//_/ .__/___/\___/\_,_/_/ \__/\__/___/ */
/* /_/ /_/ */
/* */
/* */
/******************************************************************************/
/* */
/* Titre : Récupération des données adsense et affichage de... */
/* */
/* URL : http://www.phpsources.org/scripts431-PHP.htm */
/* Auteur : forty */
/* Date édition : 23 Juil 2008 */
/* Website auteur : http://www.toplien.fr/ */
/* */
/******************************************************************************/
define('ADSENSE_TODAY',
'https://www.google.com/adsense/report/overview?timePeriod=today');
define('ADSENSE_YESTERDAY',
'https://www.google.com/adsense/report/overview?timePeriod=yesterday');
define('ADSENSE_LASTMONTH',
'https://www.google.com/adsense/report/overview?timePeriod=lastmonth');
define('ADSENSE_THISMONTH',
'https://www.google.com/adsense/report/overview?timePeriod=thismonth');
define('ADSENSE_LAST7DAYS',
'https://www.google.com/adsense/report/overview?timePeriod=last7days');
define('ADSENSE_SINCELASTPAYMENT',
'https://www.google.com/adsense/report/overview?timePeriod=sincelastpayment');
$email = "votre@email.adsense";
$password = "Votre_mot_de_passe_adsense";
function arrondi_mnt_taux($montant, $taux) {
if ($taux) {
return round($montant / $taux, 2);
} else {
return '';
}
}
class AdSense {
var $curl = null;
var $cur_url = null;
function AdSense(){
$this->curl = curl_init();
$cookie = ".";
$this->my_curl_setopt(CURLOPT_HEADER, false);
$this->my_curl_setopt(CURLOPT_RETURNTRANSFER, true);
$this->my_curl_setopt(CURLOPT_SSL_VERIFYPEER, false);
$this->my_curl_setopt(CURLOPT_SSL_VERIFYHOST, false);
$this->my_curl_setopt(CURLOPT_FOLLOWLOCATION, true);
$this->my_curl_setopt(CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$this->my_curl_setopt(CURLOPT_COOKIEFILE, $cookie);
$this->my_curl_setopt(CURLOPT_COOKIEJAR, $cookie);
register_shutdown_function(array(&$this, '__destructor'));
}
function connect($email, $password){
// phase 1
$this->cur_url = 'https://www.google.com/accounts/ServiceLoginBox';
$this->cur_url .= '?service=adsense<mpl=login&ifr=true&rm=hide&fpui=3'
;
$this->cur_url .= '&nui=15&alwf=true&passive=true';
$this->cur_url .=
'&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth';
$this->cur_url .=
'&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&hl=fr';
$this->my_curl_setopt(CURLOPT_URL, $this->cur_url);
$result = $this->my_curl_exec();
preg_match_all(
'<input[[:blank:]]+type="hidden"[[:blank:]]+name="(.*?)"[[:blank:]]+value="(.' .
'*?)">',
$result, $out);
$params = array();
foreach($out[1] as $key=>$name) { $params[] = $name . '=' . urlencode(
$out[2][$key]); }
$params[] = 'Email=' . urlencode($email);
$params[] = 'Passwd=' . urlencode($password);
$params[] = 'submit=' . urlencode('Connexion');
// phase 2
$this->cur_url = "https://www.google.com/accounts/ServiceLoginBoxAuth?"
. join('&', $params);
$this->my_curl_setopt(CURLOPT_URL,
"https://www.google.com/accounts/ServiceLoginBoxAuth");
$this->my_curl_setopt(CURLOPT_POST, true);
$this->my_curl_setopt(CURLOPT_POSTFIELDS, join('&', $params));
$result = $this->my_curl_exec();
if (preg_match("/.*<a href=\"(.*)\" target=\"_top\">/", $result,
$matches)) {
// phase 3
$this->cur_url = str_replace('&', '&', $matches[1]);
curl_setopt($this->curl, CURLOPT_POST, false);
curl_setopt($this->curl, CURLOPT_URL, $this->cur_url);
} else {
die("Impossible de récupérer l'url de redirection :<br>" . nl2br(
htmlspecialchars($result)));
}
$result = $this->my_curl_exec();
if (preg_match(
"/<meta http-equiv=\"refresh\" content=\"0; url='(.*)'\">/si", $result,
$matches )) {
// phase 4
$this->cur_url = htmlspecialchars_decode($matches[1]);
$this->my_curl_setopt(CURLOPT_URL, $this->cur_url);
$result = $this->my_curl_exec();
} else {
die("Impossible de récupérer l'url de redirection :<br>" . nl2br(
htmlspecialchars($result)));
}
// did we login ?
if (preg_match('/(' . preg_quote('/adsense/signout', '/') . '|' .
preg_quote('/adsense/gaialogout', '/') . ')/', $result)) {
return true;
} else {
//echo htmlspecialchars($result);
return false;
}
}
function today(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_TODAY);
return $this->parse($this->my_curl_exec());
}
function yesterday(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_YESTERDAY);
return $this->parse($this->my_curl_exec());
}
function last7days(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_LAST7DAYS);
return $this->parse($this->my_curl_exec());
}
function lastmonth(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_LASTMONTH);
return $this->parse($this->my_curl_exec());
}
function thismonth(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_THISMONTH);
return $this->parse($this->my_curl_exec());
}
function sincelastpayment(){
$this->my_curl_setopt(CURLOPT_URL, ADSENSE_SINCELASTPAYMENT);
return $this->parse($this->my_curl_exec());
}
function __destructor(){
$this->my_curl_close();
}
private function parse($content){
if (!preg_match_all(
'/<td nowrap valign="top" style="text-align:right" class="">(.*?)<\/td>/',
$content, $matches1)) {
echo 'Pas de détail pour :<br>' . nl2br(htmlspecialchars($content))
. '<br>';
$matches1[1][] = '';
$matches1[1][2] = '';
$matches1[1][3] = '';
$matches1[1][4] = '';
}
if (!preg_match(
'/\<td style\="font-weight\:bold;"' .
' nowrap\>([0-9,]+) \$US\<\/td\>\<\/tr\>\<\/tfoot\>\<\/table\>/',
$content, $matches2)) {
echo 'Pas de total pour :<br>' . nl2br(htmlspecialchars($content))
. '<br>';
$matches2[1] = '';
}
if (!preg_match(
'/\<span style\="color\:green;"\>([0-9,]+) \$US \<\/span\>\<\/h1\>/',
$content, $matches3)) {
echo 'Pas de revenus du jour pour :<br>' . nl2br(htmlspecialchars(
$content)) . '<br>';
$matches3[1] = '';
}
return array(
"impressions" => preg_replace('/[^0-9]/', '', $matches1[1][]),
"clicks" => preg_replace('/[^0-9]/', '', $matches1[1][1]),
"ctr" => $matches1[1][2],
"ecpm" => $matches1[1][3],
"earnings" => $matches1[1][4],
"total" => str_replace(',', '.', $matches2[1]),
"jour" => str_replace(',', '.', $matches3[1])
);
}
private function my_curl_init() {
if (($this->curl = curl_init()) === false) {
$num_erreur = curl_errno($this->curl);
$text_erreur = curl_error($this->curl);
die(
"<font color=\"red\">Erreur d'initialisation de la session cURL : $num_erreur" .
" : $text_erreur</font>");
}
}
private function my_curl_setopt($param, $value) {
if (!curl_setopt($this->curl, $param, $value)) {
$num_erreur = curl_errno($this->curl);
$text_erreur = curl_error($this->curl);
die(
"<font color=\"red\">Erreur d'ajout de l'option cURL $param pour " . $this->
cur_url .
" : $num_erreur : $text_erreur</font>");
}
}
private function my_curl_exec() {
if (($result = curl_exec($this->curl)) === false) {
$num_erreur = curl_errno($this->curl);
$text_erreur = curl_error($this->curl);
die("<font color=\"red\">Erreur d'exécution cURL pour " . $this->
cur_url .
" : $num_erreur : $text_erreur</font>");
}
$http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if (($http_code <> 200) && ($http_code <> 301) && ($http_code <> 302)) {
$url_reelle = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
die(
"<font color=\"red\">Code retour $http_code pour $url_reelle</font>");
}
return $result;
}
private function my_curl_close() {
curl_close($this->curl);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Revenus adsense</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
// code venant de :
// http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html#dev
$XMLContent= file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
);
//the file is updated daily between 14:15 and 15:00 CET
$taux = ;
foreach ($XMLContent as $line) {
if (ereg("currency='([[:alpha:]]+)'", $line, $currencyCode)) {
if (ereg("rate='([[:graph:]]+)'", $line, $rate)) {
if ($currencyCode[1] == 'USD') {
$taux = $rate[1];
}
}
}
}
$adsense = new AdSense();
if ($adsense->connect($email, $password)) {
$day = date('j');
if (($day == 1) && (date('G') >= 9)) { //nous sommes le 1er du mois
$yesterday = $adsense->yesterday();
$today = $adsense->today();
$lastmonth = $adsense->lastmonth();
echo '<table border="1">';
echo '<tr><td></td><td><strong>Impressions</strong></td>';
echo '<td><strong>Clics</strong></td>';
echo '<td><strong>CTR</strong></td>';
echo '<td><strong>eCPM</strong></td>';
echo '<td><strong>CPC</strong></td>';
echo '<td colspan="2"><strong>Montant total</strong></td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_YESTERDAY) .
'">Hier</a></strong></td>';
echo '<td>' . $yesterday['impressions'] . '</td>';
echo '<td>' . $yesterday['clicks'] . '</td>';
echo '<td>' . $yesterday['ctr'] . '</td>';
echo '<td>' . $yesterday['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($yesterday['total'], $yesterday['clicks']
) . ' $US</td>';
echo '<td>' . $yesterday['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($yesterday['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_TODAY) .
'">Aujourd\'hui</a></strong></td>';
echo '<td>' . $today['impressions'] . '</td>';
echo '<td>' . $today['clicks'] . '</td>';
echo '<td>' . $today['ctr'] . '</td>';
echo '<td>' . $today['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($today['total'], $today['clicks']) .
' $US</td>';
echo '<td>' . $today['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($today['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_LASTMONTH) .
'">Mois dernier</a></strong></td>';
echo '<td>' . $lastmonth['impressions'] . '</td>';
echo '<td>' . $lastmonth['clicks'] . '</td>';
echo '<td>' . $lastmonth['ctr'] . '</td>';
echo '<td>' . $lastmonth['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($lastmonth['total'], $lastmonth['clicks']
) . ' $US</td>';
echo '<td>' . $lastmonth['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($lastmonth['total'], $taux) .
' €</td></tr>';
echo '</table>';
} else {
$yesterday = $adsense->yesterday();
$today = $adsense->today();
$lastmonth = $adsense->lastmonth();
$thismonth = $adsense->thismonth();
if (date('G') < 9) {
$jours_hier = $day - 2;
//nombre de jours du mois jusqu'a avant hier
} else {
$jours_hier = $day - 1; //nombre de jours du mois jusqu'a hier
}
$moyenne_imp = round(($thismonth['impressions'] - $today['impressions'])
/ $jours_hier, );
$moyenne_clic = round(($thismonth['clicks'] - $today['clicks']) /
$jours_hier, );
$moyenne_ctr = round(($thismonth['clicks'] - $today['clicks']) /
($thismonth['impressions'] - $today[
'impressions']) * 100, 2);
$moyenne_ecpm = round(($thismonth['total'] - $today['total']) /
($thismonth['impressions'] - $today[
'impressions']) * 1000, 2);
$moyenne_mnt = round(($thismonth['total'] - $today['total']) /
$jours_hier, 2);
$jours_mois = date('t'); //nombre de jours du mois
$prev_imp = round(($thismonth['impressions'] - $today['impressions']) /
$jours_hier * $jours_mois, );
$prev_clic = round(($thismonth['clicks'] - $today['clicks']) /
$jours_hier * $jours_mois, );
$prev_ctr = $moyenne_ctr;
$prev_ecpm = $moyenne_ecpm;
$prev_mnt = round(($thismonth['total'] - $today['total']) / $jours_hier
* $jours_mois, 2);
echo '<table border="1">';
echo '<tr><td></td><td><strong>Impressions</strong></td>';
echo '<td><strong>Clics</strong></td>';
echo '<td><strong>CTR</strong></td>';
echo '<td><strong>eCPM</strong></td>';
echo '<td><strong>CPC</strong></td>';
echo '<td colspan="2"><strong>Montant total</strong></td></tr>';
echo '<tr><td><strong>Moyenne / jour</strong></td>';
echo '<td>' . $moyenne_imp . '</td>';
echo '<td>' . $moyenne_clic . '</td>';
echo '<td>' . $moyenne_ctr . '%</td>';
echo '<td>' . $moyenne_ecpm . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($moyenne_mnt, $moyenne_clic) .
' $US</td>';
echo '<td>' . $moyenne_mnt . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($moyenne_mnt, $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_YESTERDAY) .
'">Hier</a></strong></td>';
echo '<td>' . $yesterday['impressions'] . '</td>';
echo '<td>' . $yesterday['clicks'] . '</td>';
echo '<td>' . $yesterday['ctr'] . '</td>';
echo '<td>' . $yesterday['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($yesterday['total'], $yesterday['clicks']
) . ' $US</td>';
echo '<td>' . $yesterday['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($yesterday['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_TODAY) .
'">Aujourd\'hui</a></strong></td>';
echo '<td>' . $today['impressions'] . '</td>';
echo '<td>' . $today['clicks'] . '</td>';
echo '<td>' . $today['ctr'] . '</td>';
echo '<td>' . $today['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($today['total'], $today['clicks']) .
' $US</td>';
echo '<td>' . $today['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($today['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_LASTMONTH) .
'">Mois dernier</a></strong></td>';
echo '<td>' . $lastmonth['impressions'] . '</td>';
echo '<td>' . $lastmonth['clicks'] . '</td>';
echo '<td>' . $lastmonth['ctr'] . '</td>';
echo '<td>' . $lastmonth['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($lastmonth['total'], $lastmonth['clicks']
) . ' $US</td>';
echo '<td>' . $lastmonth['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($lastmonth['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong><a href="' . htmlspecialchars(ADSENSE_THISMONTH) .
'">Mois courant</a></strong></td>';
echo '<td>' . $thismonth['impressions'] . '</td>';
echo '<td>' . $thismonth['clicks'] . '</td>';
echo '<td>' . $thismonth['ctr'] . '</td>';
echo '<td>' . $thismonth['ecpm'] . '</td>';
echo '<td>' . arrondi_mnt_taux($thismonth['total'], $thismonth['clicks']
) . ' $US</td>';
echo '<td>' . $thismonth['total'] . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($thismonth['total'], $taux) .
' €</td></tr>';
echo '<tr><td><strong>Prévisionnel</strong></td>';
echo '<td>' . $prev_imp . '</td>';
echo '<td>' . $prev_clic . '</td>';
echo '<td>' . $prev_ctr . '%</td>';
echo '<td>' . $prev_ecpm . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($prev_mnt, $prev_clic) . ' $US</td>';
echo '<td>' . $prev_mnt . ' $US</td>';
echo '<td>' . arrondi_mnt_taux($prev_mnt, $taux) . ' €</td></tr>';
echo '</table>';
}
} else {
die('Could not login to AdSense account.');
}
?>
</body>
</html>
|