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.ini中文版(2)
Oct 09 PHP
MySQL授权问题总结
May 06 PHP
php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之间的区别
Sep 05 PHP
elgg 获取文件图标地址的方法
Mar 20 PHP
PHP 图片上传代码
Sep 13 PHP
php去除重复字的实现代码
Sep 16 PHP
PHP代码优化的53个细节
Mar 03 PHP
浅谈php冒泡排序
Dec 30 PHP
Laravel 5框架学习之表单验证
Apr 08 PHP
php curl 获取https请求的2种方法
Apr 27 PHP
在Linux系统的服务器上隐藏PHP版本号的方法
Jun 06 PHP
php无限级分类实现方法分析
Oct 19 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中多维数组按指定value排序的实现代码
2014/08/19 PHP
PHP多文件上传类实例
2015/03/07 PHP
WordPress中自定义后台管理界面配色方案的小技巧
2015/12/29 PHP
PHP会员找回密码功能的简单实现
2016/09/05 PHP
Laravel5.4框架使用socialite实现github登录的方法
2019/03/20 PHP
XRegExp 0.2: Now With Named Capture
2007/11/30 Javascript
jquery.simple.tree插件 更简单,兼容性更好的无限树插件
2010/09/03 Javascript
js获取多个tagname的节点数组
2013/09/22 Javascript
js 动态加载事件的几种方法总结
2013/12/25 Javascript
jquery实现的图片点击滚动效果
2014/04/29 Javascript
JS输入用户名自动显示邮箱后缀列表的方法
2015/01/27 Javascript
HTML5实现留言和回复页面样式
2015/07/22 Javascript
使用struts2+Ajax+jquery验证用户名是否已被注册
2016/03/22 Javascript
jQuery事件与动画基础详解
2017/02/23 Javascript
node.JS md5加密中文与php结果不一致的解决方法
2017/05/05 Javascript
JS中关于正则的巧妙操作
2017/08/31 Javascript
webpack踩坑之路图片的路径与打包
2017/09/05 Javascript
JavaScript设计模式之单例模式简单实例教程
2018/07/02 Javascript
node和vue实现商城用户地址模块
2018/12/05 Javascript
使用 JavaScript 创建并下载文件(模拟点击)
2019/10/25 Javascript
如何将Node.js中的回调转换为Promise
2020/11/10 Javascript
python 实现插入排序算法
2012/06/05 Python
初学python的操作难点总结(新手必看篇)
2017/08/03 Python
python将txt文件读取为字典的示例
2018/12/22 Python
通过shell+python实现企业微信预警
2019/03/07 Python
Python面向对象程序设计之私有属性及私有方法示例
2019/04/08 Python
Python实现 PS 图像调整中的亮度调整
2019/06/28 Python
Python实现直播推流效果
2019/11/26 Python
python 实现在shell窗口中编写print不向屏幕输出
2020/02/19 Python
Django后端分离 使用element-ui文件上传方式
2020/07/12 Python
乌克兰在线电子产品商店:MTA
2019/11/14 全球购物
小车司机岗位职责
2013/11/25 职场文书
服务员岗位责任制
2014/02/11 职场文书
营业员岗位职责
2015/02/11 职场文书
2019个人工作计划书的格式及范文!
2019/07/04 职场文书
企业开发CSS命名BEM代码规范实践
2022/02/12 HTML / CSS