PHP实现自动登入google play下载app report的方法


Posted in PHP onSeptember 23, 2014

本文实例讲述了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程序设计有一定的借鉴价值。

PHP 相关文章推荐
模拟xcopy的函数
Oct 09 PHP
MYSQL数据库初学者使用指南
Nov 16 PHP
php5.2.0内存管理改进
Jan 22 PHP
PHP编码规范-php coding standard
Mar 16 PHP
mysql 全文搜索 技巧
Apr 27 PHP
一个典型的PHP分页实例代码分享
Jul 28 PHP
PHP实现根据银行卡号判断银行
Apr 29 PHP
PHP生成随机密码方法汇总
Aug 27 PHP
php实现mysql数据库连接操作及用户管理
Nov 08 PHP
PHPStorm+XDebug进行调试图文教程
Jun 13 PHP
PHP基于DateTime类解决Unix时间戳与日期互转问题【针对1970年前及2038年后时间戳】
Jun 13 PHP
php面向对象程序设计入门教程
Jun 22 PHP
PHP遍历文件夹与文件类及处理类用法实例
Sep 23 #PHP
PHP邮件发送类PHPMailer用法实例详解
Sep 22 #PHP
php实现的CSS更新类实例
Sep 22 #PHP
php的XML文件解释类应用实例
Sep 22 #PHP
php实现的返回数据格式化类实例
Sep 22 #PHP
php实现的替换敏感字符串类实例
Sep 22 #PHP
php实现的发送带附件邮件类实例
Sep 22 #PHP
You might like
《星际争霸》各版本雷兽特点图文解析 雷兽不同形态一览
2020/03/02 星际争霸
CodeIgniter框架提示Disallowed Key Characters的解决办法
2014/04/21 PHP
php最简单的删除目录与文件实现方法
2014/11/28 PHP
四种php中webservice实现的简单架构方法及实例
2015/02/03 PHP
PHP实现图片防盗链破解操作示例【解决图片防盗链问题/反向代理】
2020/05/29 PHP
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
2021/03/09 Servers
漂亮的仿flash菜单,来自蓝色经典
2006/06/26 Javascript
jQuery图片预加载 等比缩放实现代码
2011/10/04 Javascript
3款实用的在线JS代码工具(国外)
2012/03/15 Javascript
使用Java实现简单的server/client回显功能的方法介绍
2013/05/03 Javascript
jquery实现从数组移除指定的值
2015/06/24 Javascript
学习JavaScript设计模式之观察者模式
2020/04/22 Javascript
漫谈JS引擎的运行机制 你应该知道什么
2016/06/15 Javascript
JS实现获取剪贴板内容的方法
2016/06/21 Javascript
JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
2016/09/04 Javascript
jQuery EasyUI 为Combo,Combobox添加清除值功能的实例
2017/04/13 jQuery
JS实现动态添加DOM节点和事件的方法示例
2017/04/28 Javascript
通过实例解析js简易模块加载器
2019/06/17 Javascript
详解Typescript 内置的模块导入兼容方式
2020/05/31 Javascript
vuex管理状态仓库使用详解
2020/07/29 Javascript
[06:25]DOTA2英雄梦之声_第17期_大地之灵
2014/06/20 DOTA
Python配置文件解析模块ConfigParser使用实例
2015/04/13 Python
Python中的rjust()方法使用详解
2015/05/19 Python
python实现事件驱动
2018/11/21 Python
python 数据提取及拆分的实现代码
2019/08/26 Python
简单了解python filter、map、reduce的区别
2020/01/14 Python
Python timer定时器两种常用方法解析
2020/01/20 Python
pytorch实现seq2seq时对loss进行mask的方式
2020/02/18 Python
解决Jupyter NoteBook输出的图表太小看不清问题
2020/04/16 Python
Lookfantastic日本官网:英国知名护肤、化妆品和头发护理购物网站
2018/04/21 全球购物
Mankind美国/加拿大:英国领先的男士美容护发用品公司
2018/12/05 全球购物
双十佳事迹材料
2014/01/29 职场文书
机电一体化专业毕业生自荐信
2014/06/19 职场文书
党的群众路线教育实践活动查摆剖析材料
2014/10/10 职场文书
2015年物业公司保洁工作总结
2015/10/22 职场文书
2016年教师节贺卡寄语
2015/12/04 职场文书