Forum Moderators: martinibuster
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)
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]
$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!
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]