Forum Moderators: martinibuster
There are a couple of scripts I've found, but one no longer works and the other requires Adsense Monitor installed.
I just need a basic (PHP preferred) script to securely login to my AdSense account and email the daily total to me SMS-enabled cellphone.
Does anyone know of a working script, and is this okay with the TOS?
<?
$username="uername@email.com";
$password="pass";
$cookie="/mywebsite.com/adsense/cookiefile";
$log="/mywebsite.com/adsense/adsense.log.txt";
$destination="/adsense/reports-aggregate?product=afc&dateRange.dateRangeType=simple&dateRange.simpleDate=today";
$postdata="destination=".urlencode($destination)."&username=".urlencode($username)."&password=".urlencode($password)."&null=Login";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"https://www.google.com/adsense/login.do");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
preg_match('/\<tr class\="totals"\>.*\<td.*\>.*\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*<\/tr>/simU', $result, $array);
foreach ($array as $key => $value) {$array[$key] = trim($value, "\x22\x27\n\r ");} // strip $ % , here if desired
list($full,$Impressions,$Clicks,$Rate,$CPM,$Earnings) = $array;
putenv('TZ=US/Pacific'); // match Google time no matter where you or your serverlive
$output=date("Y-m-d H:i:s")." \t ".$Impressions." \t ".$Clicks." \t ".$Rate." \t".$CPM." \t ".$Earnings."\r\n";
if ($handle = fopen($log, 'a')) {fwrite($handle, $output); fclose($handle);} else {echo "error writing";}
//mail("5555555555@mobile.att.net", "Adsense report", $output, "From: Adsense");
mail("myemail@email.com", "Adsense report", $output, "From: Adsense");
?>
I have this script, modified a bit, running every 20 minutes as a command in Geektool [projects.tynsoe.org]. So I have a small part of my desktop always showing todays, yesterdays, this months, last months and total earnings.
At the beginning
$username="uername@email.com";
$password="pass";
replace with your username and passeord
$cookie="/mywebsite.com/adsense/cookiefile";
$log="/mywebsite.com/adsense/adsense.log.txt";
path (not URL) of two files that must be writable by the php script.
easiest thing that will 99% of times work is to just have:
$cookie="cookiefile";
$log="adsense.log.txt";
The two files will have to be in the same directory where you keep the script.
create two empty files, upload them (e.g. via FTP) and then chmod them to writable
mail("myemail@email.com", ....
put your email address here
this should work!
nice script, thanks to the OP!
<?
$today = 'today';
$yesterday = 'yesterday';
$thismonth = 'thismonth';
$lastmonth = 'lastmonth';
$total = 'alltime';$html = "\n Google Adsense:\n";
$html .= ' Today: '.getUSD($today)."\n";
$html .= ' Yesterday: '.getUSD($yesterday)."\n";
$html .= ' This month: '.getUSD($thismonth)."\n";
$html .= ' Last month: '.getUSD($lastmonth)."\n";
$html .= ' Total: '.getUSD($total)."\n";
echo $html;
function getUSD($url)
{
$username = 'username'; // change this
$password = 'password'; // and this
$cookie = 'cookiefile';
$destination = '/adsense/reports-aggregate?product=afc&dateRange.dateRangeType=simple&dateRange.simpleDate='.$url;
$postdata = 'destination='.urlencode($destination).'&username='.urlencode($username).'&password='.urlencode($password).'&null=Login';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"https://www.google.com/adsense/login.do");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
preg_match('/USD ([0-9,^<]*) <\/td> <\/tr> <tr class="averages">/', $result, $match);
return $match[1];
}
?>
This will output something like:
Google Adsense:
Today: xx,xx
Yesterday: xx,xx
This month: xx,xx
Last month: xx,xx
Total: xx,xx Remember to change your username and password.
I've tried everything but I'm getting a bunch of different responses each time. The last attempt gave me this:
/home/dcmg/public_html/adsense/adsense.php: line 1:?: No such file or directory
/home/dcmg/public_html/adsense/adsense.php: line 2: =<username removed>: command not found
/home/dcmg/public_html/adsense/adsense.php: line 3: =<password removed>: command not found
/home/dcmg/public_html/adsense/adsense.php: line 4: =cookiefile: command not found
/home/dcmg/public_html/adsense/adsense.php: line 5: =adsense.log.txt: command not found
/home/dcmg/public_html/adsense/adsense.php: line 6: =/adsense/reports-aggregate?product=afc&dateRange.dateRangeType=simple&dateRange.simpleDate=today: No such file or directory
/home/dcmg/public_html/adsense/adsense.php: line 8: syntax error near unexpected token `$destination'
/home/dcmg/public_html/adsense/adsense.php: line 8: `$postdata="destination=".urlencode($destination)."&username=".urlencode($username)."&password=".urlencode($password)."&null=Login";'
What should the command be in the cron job to trigger a .php file? It seems that this is my only problem as all the tests i've done have worked at the correct time. It just isn't starting the script correctly.
Note that the actual path may be different, depending upon your server configuration.
You may also have to use the full path to the php, as in
/usr/bin/php /home/httpd/vhosts/domain.com/adsense.php >/dev/null 2>&1
(type "which php" to find where php is).
Hope this helps
Content-type: text/html
X-Powered-By: PHP/4.3.7
<br />
<b>Fatal error</b>: Call to undefined function: curl_init() in <b>/home/dcmg/public_html/adsense/adsense.php</b> on line <b>10</b><br />
Any other ideas/suggestions?
I just checked with the systemadmin and they say curl is on my account. I tried again and got the same error message:
Content-type: text/html
X-Powered-By: PHP/4.3.7
<br />
<b>Fatal error</b>: Call to undefined function: curl_init() in <b>/home/dcmg/public_html/adsense/adsense.php</b> on line <b>10</b><br />
cURL is installed on server, but may NOT be compiled into PHP. See [ca3.php.net...] for more details.
Ken
File: TodayReport.asp
<!--#include file="inc/Clshttp.asp" -->
<%
destination="/adsense/reports-aggregate?product=afc&dateRange&dateRangeType=simple&dateRange&simpleDate=today"
'
Username="YourName@yourmail.com"
Password="*********************"
CsData="destination="&server.urlencode(destination)&"&username="&server.urlencode(username)&"&password="&server.urlencode(password)&"&null=Login"
Dim myThief,ResultStr
BeginStr="<td style=""text-align:right"" nowrap class="""" valign=""top"">"
Endstr=" </tr> "
set myThief=new clsThief
myThief.src="https://www.google.com/adsense/login.do"
myThief.PData=CsData
myThief.steal
myThief.cutX BeginStr ,Endstr
ResultStr="<tr><td>"&now&"</td>"&myThief.value '
' AddtoDB resultstr
Response.write ResultStr
'Response.redirect "list.asp"
set myThief=nothing 'free object
'====================================
%>
File: Clshttp.asp
aha, this class is too large to paste here,
pls send stickymail to me if you need ,and i will send it to you by email.
I managed to get hwyking's php script working fine.
but not Tamen's
I really like the idea of getting these numbers asl well
$today = 'today';
$yesterday = 'yesterday';
$thismonth = 'thismonth';
$lastmonth = 'lastmonth';
$total = 'alltime';
did Tamen's script work for ayone else?