如何用PHP语言自动登入google play下载app report
小标 2018-07-12 来源 : 阅读 1370 评论 0

摘要:本文主要向大家介绍了如何用PHP语言自动登入google play下载app report,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了如何用PHP语言自动登入google play下载app report,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

流程
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] view plain copy

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.     /* 

31.     * @param  String $appname 

32.     * @param  String $sd            开始日期 

33.     * @param  String $ed            结束日期 

34.     * @param  String $downloadFile  保存的zip名称 

35.     */  

36.     public function run($appname='', $sd='', $ed='', $downloadFile=''){  

37.           

38.         $package = $appname;  

39.         $dim = 'overall,country,language,os_version,device,app_version,carrier';  

40.         //$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';  

41.         $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  

42.       

43.         // login google play  

44.         $this->loginAuth($this->username, $this->password);  

45.   

46.         // download report zip  

47.         return $this->downloadReport($package, $sd, $ed, $dim, $met, $this->dev_acc, $downloadFile);  

48.       

49.     }  

50.   

51.   

52.     /* login google play,create cookies 

53.     * @param  String $username 

54.     * @param  String $password  

55.     * @return boolean 

56.     */  

57.     private function loginAuth($username, $password){  

58.           

59.         // step1  

60.         $mainUrl = "https://play.google.com/apps/publish/";  

61.   

62.         $ch = curl_init();  

63.         curl_setopt($ch, CURLOPT_URL, $mainUrl);  

64.         curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE);  

65.         curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);   

66.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

67.         curl_exec($ch);  

68.         curl_close($ch);  

69.   

70.         // step 2  

71.         $serviceLoginUrl = "https://accounts.google.com/ServiceLogin?hl=en&continue=".$mainUrl;  

72.         $ch = curl_init();  

73.         curl_setopt($ch, CURLOPT_URL, $serviceLoginUrl);  

74.         curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE);  

75.         curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);   

76.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

77.         $serviceLoginRespHtml = curl_exec($ch);  

78.         curl_close($ch);  

79.   

80.         preg_match('/name="dsh"\s*id="dsh"\s*value="(.*?)"\s*/i', $serviceLoginRespHtml, $matches); // get dsh  

81.         $dsh = $matches[1];  

82.   

83.         preg_match('/name="GALX"\s*value="(.*?)"\s*/i', $serviceLoginRespHtml, $matches); // get GALX  

84.         $galx = $matches[1];  

85.   

86.         // step 3  

87.         $loginGoogleUrl = "https://accounts.google.com/ServiceLoginAuth";  

88.         $postFields = "Referer=".$serviceLoginUrl;  

89.         $postFields .= "&AllowAutoRedirect=false";  

90.         $postFields .= "&continue=".$mainUrl;  

91.         $postFields .= "&dsh=".$dsh;  

92.         $postFields .= "&h1=en";  

93.         $postFields .= "&GALX=".$galx;  

94.         $postFields .= "&Email=".$username;  

95.         $postFields .= "&Passwd=".$password;  

96.         $postFields .= "&signIn=Sign+in";  

97.         $postFields .= "&PersistentCookie=yes";  

98.           

99.         $ch = curl_init();  

100.         curl_setopt($ch, CURLOPT_URL, $loginGoogleUrl);  

101.         curl_setopt($ch, CURLOPT_POST, 1);  

102.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);  

103.         curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_PLAY_COOKIE_FILE);  

104.         curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);   

105.         curl_setopt($ch, CURLOPT_HEADER, true);   

106.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);   

107.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

108.         curl_exec($ch);  

109.         curl_close($ch);  

110.   

111.         // login cookies create success  

112.         return true;  

113.       

114.     }  

115.   

116.   

117.     // download Report zip file  

118.     private function downloadReport($package, $sd, $ed, $dim, $met, $dev_acc, $downloadFile) {  

119.   

120.         $url = "https://play.google.com/apps/publish/statistics/download?package={$package}&sd={$sd}&ed={$ed}&dim={$dim}&met={$met}&dev_acc={$dev_acc}";  

121.           

122.         $fp = fopen($downloadFile,"w");  

123.   

124.         $ch = curl_init();   

125.         curl_setopt($ch, CURLOPT_URL, $url);  

126.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   

127.         curl_setopt($ch, CURLOPT_FILE, $fp);  

128.         curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_PLAY_COOKIE_FILE);   

129.         curl_exec($ch);   

130.         curl_close($ch);   

131.         fclose($fp);  

132.   

133.         if (file_exists($downloadFile)){  

134.             return true;  

135.         }  

136.       

137.         return false;  

138.   

139.     }  

140.   

141.   

142.     /* unzip report 

143.     * @param String $path         解压的路径 

144.     * @param String $downloadFile zip file 

145.     */  

146.     public function unzipReport($path, $downloadFile){  

147.         $exec = "unzip ".$downloadFile. " -d ".$path;  

148.         shell_exec($exec);  

149.         unlink($downloadFile);  // delete zip file  

150.     }  

151.   

152. }  

153.   

154.   

155. // demo  

156. $username = 'testdev@gmail.com';  

157. $password = 'abcd1234';  

158. $dev_acc = '12345678901234567890';  

159.   

160. $appname = 'com.testdev';  

161. $sd = '20130417';  

162. $ed = '20130417';  

163. $downloadFile = 'testdev.zip';  

164. $unzipPath = ROOT_PATH.'/testdev/';  

165.   

166. $obj = new AndroidReportDownLoader($username, $password, $dev_acc);  

167. if($obj->run($appname, $sd, $ed, $downloadFile)){  

168.     $obj->unzipReport($unzipPath, $downloadFile);  

169. }  

170.   

171. ?>  

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程