当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-14 11:17:17 浏览: 评论:0 

这篇文章主要介绍了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

二、实现代码如下:

  1. <?php 
  2. define('ROOT_PATH', dirname(__FILE__)); 
  3. define('GOOGLE_PLAY_COOKIE_FILE', 'google_play_cookie.txt'); 
  4.  
  5. /** 
  6. * Login google play, download report, unzip 
  7. * Date:   2013-04-17 
  8. * Author:  fdipzone 
  9. * Version: 1.0 
  10. */ 
  11. class AndroidReportDownLoader{ 
  12.  
  13.   private $username; 
  14.   private $password; 
  15.   private $dev_acc; 
  16.  
  17.  
  18.   /* init 
  19.   * @param String $username google play account 
  20.   * @param String $password google play password 
  21.   * @param String $dev_acc google play dev account 
  22.   */ 
  23.   public function __construct($username='', $password='', $dev_acc=''){ 
  24.     $this->username = $username; 
  25.     $this->password = $password; 
  26.     $this->dev_acc = $dev_acc; 
  27.   } 
  28.  
  29.   /* 
  30.   * @param String $appname 
  31.   * @param String $sd      开始日期 
  32.   * @param String $ed      结束日期 
  33.   * @param String $downloadFile 保存的zip名称 
  34.   */ 
  35.   public function run($appname='', $sd='', $ed='', $downloadFile=''){ 
  36.       
  37.     $package = $appname; 
  38.     $dim = 'overall,country,language,os_version,device,app_version,carrier'; 
  39.     //$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'; 
  40.     $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 
  41.     
  42.     // login google play 
  43.     $this->loginAuth($this->username, $this->password); 
  44.  
  45.     // download report zip 
  46.     return $this->downloadReport($package, $sd, $ed, $dim, $met, $this->dev_acc, $downloadFile); 
  47.     
  48.   } 
  49.  
  50.   /* login google play,create cookies 
  51.   * @param String $username 
  52.   * @param String $password  
  53.   * @return boolean 
  54.   */ 
  55.   private function loginAuth($username, $password){ 
  56.       
  57.     // step1 
  58.     $mainUrl = "https://play.google.com/apps/publish/"; 
  59.  
  60.     $ch = curl_init(); 
  61.     curl_setopt($ch, CURLOPT_URL, $mainUrl); 
  62.     curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE); 
  63.     curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);  
  64.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  65.     curl_exec($ch); 
  66.     curl_close($ch); 
  67.  
  68.     // step 2 
  69.     $serviceLoginUrl = "https://accounts.google.com/ServiceLogin?hl=en&continue=".$mainUrl; 
  70.     $ch = curl_init(); 
  71.     curl_setopt($ch, CURLOPT_URL, $serviceLoginUrl); 
  72.     curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE); 
  73.     curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);  
  74.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  75.     $serviceLoginRespHtml = curl_exec($ch); 
  76.     curl_close($ch); 
  77.  
  78.     preg_match('/name="dsh"\s*id="dsh"\s*value="(.*?)"\s*/i', $serviceLoginRespHtml, $matches); // get dsh 
  79.     $dsh = $matches[1]; 
  80.  
  81.     preg_match('/name="GALX"\s*value="(.*?)"\s*/i', $serviceLoginRespHtml, $matches); // get GALX 
  82.     $galx = $matches[1]; 
  83.  
  84.     // step 3 
  85.     $loginGoogleUrl = "https://accounts.google.com/ServiceLoginAuth"; 
  86.     $postFields = "Referer=".$serviceLoginUrl; 
  87.     $postFields .= "&AllowAutoRedirect=false"; 
  88.     $postFields .= "&continue=".$mainUrl; 
  89.     $postFields .= "&dsh=".$dsh; 
  90.     $postFields .= "&h1=en"; 
  91.     $postFields .= "&GALX=".$galx; 
  92.     $postFields .= "&Email=".$username; 
  93.     $postFields .= "&Passwd=".$password; 
  94.     $postFields .= "&signIn=Sign+in"; 
  95.     $postFields .= "&PersistentCookie=yes"; 
  96.       
  97.     $ch = curl_init(); 
  98.     curl_setopt($ch, CURLOPT_URL, $loginGoogleUrl); 
  99.     curl_setopt($ch, CURLOPT_POST, 1); 
  100.     curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 
  101.     curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE); 
  102.     curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);  
  103.     curl_setopt($ch, CURLOPT_HEADER, true);  
  104.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
  105.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  106.     curl_exec($ch); 
  107.     curl_close($ch); 
  108.  
  109.     // login cookies create success 
  110.     return true; 
  111.   } 
  112.  
  113.   // download Report zip file 
  114.   private function downloadReport($package, $sd, $ed, $dim, $met, $dev_acc, $downloadFile) { 
  115.  
  116.     $url = "https://play.google.com/apps/publish/statistics/download?package={$package}&sd={$sd}&ed={$ed}&dim={$dim}&met={$met}&dev_acc={$dev_acc}"; 
  117.       
  118.     $fp = fopen($downloadFile,"w"); 
  119.  
  120.     $ch = curl_init();  
  121.     curl_setopt($ch, CURLOPT_URL, $url); 
  122.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  123.     curl_setopt($ch, CURLOPT_FILE, $fp); 
  124.     curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);  
  125.     curl_exec($ch);  
  126.     curl_close($ch);  
  127.     fclose($fp); 
  128.  
  129.     if (file_exists($downloadFile)){ 
  130.       return true; 
  131.     } 
  132.     return false; 
  133.  
  134.   } 
  135.  
  136.   /* unzip report 
  137.   * @param String $path     解压的路径 
  138.   * @param String $downloadFile zip file 
  139.   */ 
  140.   public function unzipReport($path, $downloadFile){ 
  141.     $exec = "unzip ".$downloadFile. " -d ".$path; 
  142.     shell_exec($exec); 
  143.     unlink($downloadFile); // delete zip file 
  144.   } 
  145. } 
  146.  
  147. // demo 
  148. $username = 'testdev@gmail.com'; 
  149. $password = 'abcd1234'; 
  150. $dev_acc = '12345678901234567890'; 
  151. //www.phpfensi.com 
  152. $appname = 'com.testdev'; 
  153. $sd = '20130417'; 
  154. $ed = '20130417'; 
  155. $downloadFile = 'testdev.zip'; 
  156. $unzipPath = ROOT_PATH.'/testdev/'; 
  157.  
  158. $obj = new AndroidReportDownLoader($username, $password, $dev_acc); 
  159. if($obj->run($appname, $sd, $ed, $downloadFile)){ 
  160.   $obj->unzipReport($unzipPath, $downloadFile); 
  161. } 
  162. ?> 

相信本文所述对大家的PHP程序设计有一定的借鉴价值。

Tags: PHP自动登入google

分享到: