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 相关文章推荐
让你成为更出色的PHP开发者的10个技巧
Feb 25 PHP
PHP基础知识回顾
Aug 16 PHP
PHP连接MongoDB示例代码
Sep 06 PHP
php防注入,表单提交值转义的实现详解
Jun 10 PHP
Thinkphp中数据按分类嵌套循环实现方法
Oct 30 PHP
PHP使用PHPexcel导入导出数据的方法
Nov 14 PHP
php+jQuery+Ajax实现点赞效果的方法(附源码下载)
Jul 21 PHP
php中让人头疼的浮点数运算分析
Oct 10 PHP
PHP命名空间namespace的定义方法详解
Mar 29 PHP
PHP实现的mysql主从数据库状态检测功能示例
Jul 20 PHP
php批量转换文件夹下所有文件编码的函数类
Aug 06 PHP
YII框架常用技巧总结
Apr 27 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
CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能
2014/07/18 PHP
jQuery获取json后使用zy_tmpl生成下拉菜单
2015/03/27 PHP
PHP基于单例模式实现的mysql类
2016/01/09 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
2016/09/21 PHP
基于PHP实现用户注册登录功能
2016/10/14 PHP
可缩放Reloaded-一个针对可缩放元素的复用组件
2007/03/10 Javascript
解析javascript 实用函数的使用详解
2013/05/10 Javascript
alert中断settimeout计时功能
2013/07/26 Javascript
jQuery+AJAX实现遮罩层登录验证界面(附源码)
2020/09/13 Javascript
react-native android状态栏的实现
2018/06/15 Javascript
详解vue项目打包步骤
2019/03/29 Javascript
基于JS实现前端压缩上传图片的实例代码
2019/05/14 Javascript
Ant Design Vue 添加区分中英文的长度校验功能
2020/01/21 Javascript
vue-drawer-layout实现手势滑出菜单栏
2020/11/19 Vue.js
Javascript生成器(Generator)的介绍与使用
2021/01/31 Javascript
python 集合 并集、交集 Series list set 转换的实例
2018/05/29 Python
基于windows下pip安装python模块时报错总结
2018/06/12 Python
Python基于多线程操作数据库相关问题分析
2018/07/11 Python
使用Python如何测试InnoDB与MyISAM的读写性能
2018/09/18 Python
使用Python制作一个打字训练小工具
2019/10/01 Python
基于h5py的使用及数据封装代码
2019/12/26 Python
Python sorted排序方法如何实现
2020/03/31 Python
Django haystack实现全文搜索代码示例
2020/11/28 Python
详解Django关于StreamingHttpResponse与FileResponse文件下载的最优方法
2021/01/07 Python
纯CSS3实现的阴影效果
2014/12/24 HTML / CSS
The Athlete’s Foot新西兰:新西兰最大的运动鞋零售商
2019/12/23 全球购物
研究生自我鉴定范文
2013/10/30 职场文书
硕士研究生自我鉴定
2013/11/08 职场文书
腾讯广告词
2014/03/19 职场文书
企业活动策划方案
2014/06/02 职场文书
2014年学校办公室工作总结
2014/12/19 职场文书
离婚答辩状怎么写
2015/05/22 职场文书
整脏治乱工作简报
2015/07/21 职场文书
结婚十年感言
2015/07/31 职场文书
2016年大学生党员承诺书
2016/03/24 职场文书
让JavaScript代码更加精简的方法技巧
2022/06/01 Javascript