本文实例讲述了php实现自动登入google play下载app report的方法,有不错的实用价值。分享给大家供大家参考。具体实现步骤如下:
一、流程:
1.登入google play
登入google play需要三步:
https://play.google.com/apps/publish/
https://accounts.google.com/servicelogin?hl=en&continue=https://play.google.com/apps/publish/
https://accounts.google.com/serviceloginauth
2.下载app report zip
3.unzip report
二、实现代码如下:
<?php
define('root_path', dirname(__file__));
define('google_play_cookie_file', 'google_play_cookie.txt');
/**
* login google play, download report, unzip
* date: 2013-04-17
* author: fdipzone
* version: 1.0
*/
class androidreportdownloader{
private $username;
private $password;
private $dev_acc;
/* init
* @param string $username google play account
* @param string $password google play password
* @param string $dev_acc google play dev account
*/
public function __construct($username='', $password='', $dev_acc=''){
$this->username = $username;
$this->password = $password;
$this->dev_acc = $dev_acc;
}
/*
* @param string $appname
* @param string $sd 开始日期
* @param string $ed 结束日期
* @param string $downloadfile 保存的zip名称
*/
public function run($appname='', $sd='', $ed='', $downloadfile=''){
$package = $appname;
$dim = 'overall,country,language,os_version,device,app_version,carrier';
//$met = 'daily_device_installs,active_device_installs,daily_user_installs,total_user_installs,active_user_installs,daily_device_uninstalls,daily_user_uninstalls,daily_device_upgrades';
$met = "daily_device_installs,current_device_installs,daily_user_installs,total_user_installs,current_user_installs,daily_device_uninstalls,daily_user_uninstalls,daily_device_upgrades"; // google modify 2013-08-06
// login google play
$this->loginauth($this->username, $this->password);
// download report zip
return $this->downloadreport($package, $sd, $ed, $dim, $met, $this->dev_acc, $downloadfile);
}
/* login google play,create cookies
* @param string $username
* @param string $password
* @return boolean
*/
private function loginauth($username, $password){
// step1
$mainurl = "https://play.google.com/apps/publish/";
$ch = curl_init();
curl_setopt($ch, curlopt_url, $mainurl);
curl_setopt($ch, curlopt_cookiejar, google_play_cookie_file);
curl_setopt($ch, curlopt_cookiefile, google_play_cookie_file);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_exec($ch);
curl_close($ch);
// step 2
$serviceloginurl = "https://accounts.google.com/servicelogin?hl=en&continue=".$mainurl;
$ch = curl_init();
curl_setopt($ch, curlopt_url, $serviceloginurl);
curl_setopt($ch, curlopt_cookiejar, google_play_cookie_file);
curl_setopt($ch, curlopt_cookiefile, google_play_cookie_file);
curl_setopt($ch, curlopt_returntransfer, 1);
$serviceloginresphtml = curl_exec($ch);
curl_close($ch);
preg_match('/name="dsh"s*id="dsh"s*value="(.*?)"s*/i', $serviceloginresphtml, $matches); // get dsh
$dsh = $matches[1];
preg_match('/name="galx"s*value="(.*?)"s*/i', $serviceloginresphtml, $matches); // get galx
$galx = $matches[1];
// step 3
$logingoogleurl = "https://accounts.google.com/serviceloginauth";
$postfields = "referer=".$serviceloginurl;
$postfields .= "&allowautoredirect=false";
$postfields .= "&continue=".$mainurl;
$postfields .= "&dsh=".$dsh;
$postfields .= "&h1=en";
$postfields .= "&galx=".$galx;
$postfields .= "&email=".$username;
$postfields .= "&passwd=".$password;
$postfields .= "&signin=sign+in";
$postfields .= "&persistentcookie=yes";
$ch = curl_init();
curl_setopt($ch, curlopt_url, $logingoogleurl);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $postfields);
curl_setopt($ch, curlopt_cookiejar, google_play_cookie_file);
curl_setopt($ch, curlopt_cookiefile, google_play_cookie_file);
curl_setopt($ch, curlopt_header, true);
curl_setopt($ch, curlopt_followlocation, true);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_exec($ch);
curl_close($ch);
// login cookies create success
return true;
}
// download report zip file
private function downloadreport($package, $sd, $ed, $dim, $met, $dev_acc, $downloadfile) {
$url = "https://play.google.com/apps/publish/statistics/download?package={$package}&sd={$sd}&ed={$ed}&dim={$dim}&met={$met}&dev_acc={$dev_acc}";
$fp = fopen($downloadfile,"w");
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_file, $fp);
curl_setopt($ch, curlopt_cookiefile, google_play_cookie_file);
curl_exec($ch);
curl_close($ch);
fclose($fp);
if (file_exists($downloadfile)){
return true;
}
return false;
}
/* unzip report
* @param string $path 解压的路径
* @param string $downloadfile zip file
*/
public function unzipreport($path, $downloadfile){
$exec = "unzip ".$downloadfile. " -d ".$path;
shell_exec($exec);
unlink($downloadfile); // delete zip file
}
}
// demo
$username = 'testdev@gmail.com';
$password = 'abcd1234';
$dev_acc = '12345678901234567890';
$appname = 'com.testdev';
$sd = '20130417';
$ed = '20130417';
$downloadfile = 'testdev.zip';
$unzippath = root_path.'/testdev/';
$obj = new androidreportdownloader($username, $password, $dev_acc);
if($obj->run($appname, $sd, $ed, $downloadfile)){
$obj->unzipreport($unzippath, $downloadfile);
}
?>
相信本文所述对大家的php程序设计有一定的借鉴价值。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!