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

 更新时间:2014年09月23日 09:14:56   投稿:shichen2014  
这篇文章主要介绍了PHP实现自动登入google play下载app report的方法,较为详细的讲述了登陆下载APP及对应的实现代码,具有不错的实用价值,需要的朋友可以参考下

本文实例讲述了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中限制IP段访问、禁止IP提交表单的代码

    PHP中限制IP段访问、禁止IP提交表单的代码

    最近,小编发现有一个云南的网友经常在网站发表一些垃圾信息的评论,由于使用的事DEDECMS构架,系统本身并无禁止IP功能,每天看到这些垃圾评论,尽管不多,但是让人感觉不爽,那么如何来限制呢?
    2011-04-04
  • 解析PHP 5.5 新特性

    解析PHP 5.5 新特性

    本篇文章是对PHP 5.5 的新特性进行了详细的分析介绍,需要的朋友参考下
    2013-07-07
  • 利用PHP内置SERVER开启web服务(本地开发使用)

    利用PHP内置SERVER开启web服务(本地开发使用)

    PHP 5.4.0起, CLI SAPI 提供了一个内置的Web服务器,这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境,URI请求会被发送到PHP所在的的工作目录Working Directory进行处理,除非你使用了-t参数来自定义不同的目录
    2020-01-01
  • 老生常谈PHP位运算的用途

    老生常谈PHP位运算的用途

    下面小编就为大家带来一篇老生常谈PHP位运算的用途。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 浅谈如何提高PHP代码质量之单元测试

    浅谈如何提高PHP代码质量之单元测试

    最常见的测试软件的方法可能是编写单元测试。它们的目的是测试代码的特定单元,基于这样的假设:一切都按预期运行。为了能够编写适当的单元测试,我们的代码应该遵循一些基本的设计规则。我们应该特别关注 SOLID 原则。
    2021-05-05
  • 深入extjs与php参数交互的详解

    深入extjs与php参数交互的详解

    本篇文章是对extjs与php的参数交互进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php模拟用户自动在qq空间发表文章的方法

    php模拟用户自动在qq空间发表文章的方法

    这篇文章主要介绍了php模拟用户自动在qq空间发表文章的方法,可实现模拟用户提交表单发布文章的功能,代码中包含有较为详尽的注释便于理解,需要的朋友可以参考下
    2015-01-01
  • 在WINDOWS中设置计划任务执行PHP文件的方法

    在WINDOWS中设置计划任务执行PHP文件的方法

    在网上找了些WINDOWS执行PHP的计划任务的方法,有一个写得很全,可惜在我这竟然没通过。最后不得不综合各门派的方法,才能在我这运行成功
    2011-12-12
  • php调用mysql存储过程

    php调用mysql存储过程

    php调用mysql存储过程...
    2007-02-02
  • php新浪微博登录接口用法实例

    php新浪微博登录接口用法实例

    这篇文章主要介绍了php新浪微博登录接口用法,以实例形式分析了新浪微博接口的申请与具体使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12

最新评论