Forum Moderators: martinibuster

Message Too Old, No Replies

Adsense PHP Login Script?

         

wibble99

4:18 pm on Aug 25, 2008 (gmt 0)

10+ Year Member



Has anyone got the code for a PHP script that will log into Adsense, so the stats can be screenscraped? I want to be able to automate it so I can collate my Adsense earnings with my other earnings, but while I used to be able to automate logging into Adsense (I did it some time ago now) I can't figure it out now it's got more complicated!

I've spent all day trying, but haven't managed it. I found the following thread with code for logging into Adsense, but it doesn't appear to work any more:

[webmasterworld.com...]

Can anyone help?

(By the way - I'm not looking for some software that does it, I'm wanting the PHP code so that I can use it in my own programs)

amznVibe

6:07 am on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My script is still working. I use it every day. Just don't poll Google too often.
Make sure you replace the curl parts with this updated version:
[webmasterworld.com...]

the original discussion was over here:
[webmasterworld.com...]

the final pass by curl grabs the newest CSV so you can just parse it like this:


<?php
$array=preg_split('/\n/simU',preg_replace('/[\x00]¦[\x80-\xFF]/simU','',$result));
list($full,$Impressions,$Clicks,$Rate,$CPM,$Earnings)=preg_split('/\t/simU', $array[count($array)-3]);

putenv('TZ=US/Pacific'); // match Google time no matter where you or your server live
$output=date("Y-m-d")."\t".date("H:i:s")."\t".$Impressions."\t".$Clicks."\t".$Rate."\t$".$CPM."\t$".$Earnings."\r\n";
?>

[edited by: amznVibe at 6:13 am (utc) on Aug. 26, 2008]

amznVibe

7:20 am on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember to replace the "¦" pipe symbol as this forum software tampers with it.

wibble99

8:54 am on Aug 26, 2008 (gmt 0)

10+ Year Member



Thanks amznVibe, but it's still not working for me so maybe I'm doing something wrong. I'm using the following (copied direct from your post on another thread)


$username="your-username";
$password="your-password";
$postdata="Email=".urlencode($username)."&Passwd=%09".urlencode($password)."&service=adsense&ifr=true&rmShown=1&null=Sign+in";
$agent="User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
$cookie=dirname(__FILE__)."/cookiefile";
putenv('TZ=US/Pacific'); $randate=mktime(0, 0, 0, date("m"), date("d")-rand(3,26),date("Y"));


$chain=array(
"https://www.google.com/accounts/ServiceLoginBoxAuth",
"https://www.google.com/accounts/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&service=adsense&hl=en_US&chtml=LoginDoneHtml",
"https://www.google.com/adsense/report/aggregate?sortColumn=0&reverseSort=false&storedReportId=-1&isOldReport=false&product=afc&dateRange.simpleDate=thismonth&dateRange.dateRangeType=custom&dateRange.customDate.start.month="
.date("n",$randate)."&dateRange.customDate.start.day=".date("j",$randate)."&dateRange.customDate.start.year=".date("Y",$randate)."&dateRange.customDate.end.month=".date("n")."&dateRange.customDate.end.day=".date("j")."&dateRange.customDate.end.year=".date("Y")
."&unitPref=page&reportType=property&searchField=&groupByPref=date&csv=true&outputFormat=TSV_EXCEL"
);


$ch = curl_init();
foreach ($chain as $url)
{
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 35);
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);
// echo "<html>".$result;

Though obviously putting my own username and password in.

When it performs the first curl_exec the page returned is a smallish page that's trying to redirect the user to the CheckCookie page.

The 2nd pass (when getting the CheckCookie URL) returns a small page that tries to redirect the user to google.co.uk/accounts/SetSID?....

The third pass, however, just returns the standard Google Adsense Welcome page, telling you what Google Adsense is. I.e. it appears to be what you see when you visit [google.com...] without being logged in.

I'll have a look further, as it may be complicated by me being British (so maybe needing en_GB rather than en_US) or something else like that.

Also, I'm testing it on my PC and it could be there's a setup problem. I've just tried it on my reseller account, though, and I get an error saying:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ....

This has turned out to be a lot more complicated than I was initially expecting!

Thanks for your help, though!

wibble99

11:43 am on Aug 26, 2008 (gmt 0)

10+ Year Member



Ah - think I've cracked it, though my code's not neat so I'll explain.

After the 2nd page is run (the CheckCookie URL) I then extract out of the returned page the URL that it is trying to redirect me to, and I then fetch that.

I'm then logged in ok and I can get my current earnings. Phew!

amznVibe

12:46 pm on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Proper cookie storage and follow location is critical for the script to work but glad you figured it out in the end.

You've basically learned that its fairly straightforward to write some code to replace the followlocation feature but cookie storage is a must. I'm not sure why curl disables the followlocation when it's so easy to emulate but I guess it might be for local urls that resolve to resources or something like that.

[edited by: amznVibe at 12:47 pm (utc) on Aug. 26, 2008]