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 相关文章推荐
vBulletin Forum 2.3.xx SQL Injection
Oct 09 PHP
dedecms模版制作使用方法
Apr 03 PHP
php操作xml
Oct 27 PHP
PHP针对JSON操作实例分析
Jan 12 PHP
php实现统计网站在线人数的方法
May 12 PHP
PHP文件操作方法汇总
Jul 01 PHP
js基于qrcode.js生成二维码的方法【附demo插件源码下载】
Dec 28 PHP
微信公众号开发之获取位置信息php代码
Jun 13 PHP
php下的原生ajax请求用法实例分析
Feb 28 PHP
PHP Trait功能与用法实例分析
Jun 03 PHP
Yii 实现数据加密和解密
Mar 09 PHP
PHP7 错误处理机制修改
Mar 09 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
深入理解用mysql_fetch_row()以数组的形式返回查询结果
2013/06/05 PHP
Codeigniter实现处理用户登录验证后的URL跳转
2014/06/12 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
2015/06/19 PHP
PHP实现操作redis的封装类完整实例
2015/11/14 PHP
PHP的swoole扩展安装方法详细教程
2016/05/18 PHP
js类中获取外部函数名的方法与代码
2007/09/12 Javascript
仅IE支持clearAttributes/mergeAttributes方法使用介绍
2012/05/04 Javascript
javascript模拟地球旋转效果代码实例
2013/12/02 Javascript
js中继承的几种用法总结(apply,call,prototype)
2013/12/26 Javascript
javascript获取和判断浏览器窗口、屏幕、网页的高度、宽度等
2014/05/08 Javascript
一个字符串反转函数可实现字符串倒序
2014/09/15 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
nodejs实现bigpipe异步加载页面方案
2016/01/26 NodeJs
Bootstrap每天必学之日期控制
2016/03/07 Javascript
jQuery on()方法绑定动态元素的点击事件实例代码浅析
2016/06/16 Javascript
JS时间控制实现动态效果的实例讲解
2017/07/31 Javascript
for循环 + setTimeout 结合一些示例(前端面试题)
2017/08/30 Javascript
浅谈Angular4中常用管道
2017/09/27 Javascript
移动web开发之touch事件实例详解
2018/01/17 Javascript
解决Vue axios post请求,后台获取不到数据的问题方法
2018/08/11 Javascript
小程序实现页面顶部选项卡效果
2018/11/06 Javascript
浅谈Vue CLI 3结合Lerna进行UI框架设计
2019/04/14 Javascript
微信小程序实现左侧滑动导航栏
2020/04/08 Javascript
Pyinstaller将py打包成exe的实例
2018/03/31 Python
Python3多进程 multiprocessing 模块实例详解
2018/06/11 Python
用sqlalchemy构建Django连接池的实例
2019/08/29 Python
python之array赋值技巧分享
2019/11/28 Python
测试工程师岗位职责
2013/11/28 职场文书
微博营销计划书
2014/01/10 职场文书
护士节策划方案
2014/05/19 职场文书
宾馆仓管员岗位职责
2014/07/27 职场文书
村主任群众路线个人对照检查材料
2014/09/26 职场文书
党委书记群众路线对照检查材料思想汇报
2014/10/04 职场文书
2016年暑期见闻作文
2015/11/25 职场文书
MySQL中连接查询和子查询的问题
2021/09/04 MySQL
教你在 Java 中实现 Dijkstra 最短路算法的方法
2022/04/08 Java/Android