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 相关文章推荐
Apache设置虚拟WEB
Oct 09 PHP
用Php实现链结人气统计
Oct 09 PHP
深入mysql_fetch_row()与mysql_fetch_array()的区别详解
Jun 05 PHP
PHP不用递归实现无限分级的例子分享
Apr 18 PHP
php实现redis数据库指定库号迁移的方法
Jan 14 PHP
php生成圆角图片的方法
Apr 07 PHP
php使用Jpgraph绘制3D饼状图的方法
Jun 10 PHP
PHP 读取大文件并显示的简单实例(推荐)
Aug 12 PHP
Yii1.1中通过Sql查询进行的分页操作方法
Mar 16 PHP
php多进程模拟并发事务产生的问题小结
Dec 07 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
Feb 27 PHP
PHP filter_var() 函数, 验证判断EMAIL,URL等
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
PHP读取CURL模拟登录时生成Cookie文件的方法
2014/11/04 PHP
PHP Class SoapClient not found解决方法
2018/01/20 PHP
juqery 学习之三 选择器 可见性 元素属性
2010/11/25 Javascript
javaScript函数中执行C#代码中的函数方法总结
2013/08/07 Javascript
jquery动态增加删除表格行的小例子
2013/11/14 Javascript
js遍历子节点子元素附属性及方法
2014/08/19 Javascript
jQuery响应鼠标事件并隐藏与显示input默认值
2014/08/24 Javascript
常用的JavaScript WEB操作方法分享
2015/02/28 Javascript
使用Javascript写的2048小游戏
2015/11/25 Javascript
JavaScript时间操作之年月日星期级联操作
2016/01/15 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
JS获取当前页面名称的简单实例
2016/08/19 Javascript
Node.js开启Https的实践详解
2016/10/25 Javascript
jQuery Validate让普通按钮触发表单验证的方法
2016/12/15 Javascript
详解本地Node.js服务器作为api服务器的解决办法
2017/02/28 Javascript
在 Angular中 使用 Lodash 的方法
2018/02/11 Javascript
node使用promise替代回调函数
2018/05/07 Javascript
koa+mongoose实现简单增删改查接口的示例代码
2019/05/13 Javascript
浅谈layui数据表格判断问题(加入表单元素),设置单元格样式
2019/10/26 Javascript
vue-cli3项目打包后自动化部署到服务器的方法
2020/09/16 Javascript
使用Python对SQLite数据库操作
2017/04/06 Python
利用Python如何制作好玩的GIF动图详解
2018/07/11 Python
[原创]Python入门教程5. 字典基本操作【定义、运算、常用函数】
2018/11/01 Python
基于python实现的百度音乐下载器python pyqt改进版(附代码)
2019/08/05 Python
浅谈Python_Openpyxl使用(最全总结)
2019/09/05 Python
PyTorch实现AlexNet示例
2020/01/14 Python
jupyter lab文件导出/下载方式
2020/04/22 Python
Emporio Armani腕表天猫官方旗舰店:乔治·阿玛尼为年轻人设计的副线品牌
2017/07/02 全球购物
教师考核评语
2014/04/28 职场文书
教师敬业奉献模范事迹材料
2014/05/18 职场文书
公司收款委托书范本
2014/09/20 职场文书
班级光棍节联谊会策划书
2014/10/10 职场文书
2015民办小学年度工作总结
2015/05/26 职场文书
奖学金主要事迹范文
2015/11/04 职场文书
opencv读取视频并保存图像的方法
2021/06/04 Python
详解Redis在SpringBoot工程中的综合应用
2021/10/16 Redis