Google Play 開發者後台提供 App 安裝/解除安裝/收入等統計報表,支援 CSV 下載。本文實作 AndroidReportDownLoader 類別,模擬瀏覽器完成 Google 帳號登入(三步驟 OAuth 流程)、取得 Cookie、依套件名稱/日期範圍/維度/指標下載壓縮報表、自動解壓縮,適合納入自動化資料管線(Cron、ETL)。
GET https://play.google.com/apps/publish/ → 取得初始 Cookie、導向登入頁。GET https://accounts.google.com/ServiceLogin?hl=en&continue=... → 解析表單隱藏欄位 dsh、GALX。POST https://accounts.google.com/ServiceLoginAuth → 提交 Email、Passwd、隱藏欄位,取得認證 Cookie(SID、HSID、SSID 等)。<?php
define('GOOGLE_PLAY_COOKIE_FILE', 'google_play_cookie.txt');
class AndroidReportDownLoader {
private $username, $password, $dev_acc;
public function __construct($username, $password, $dev_acc) {
$this->username=$username; $this->password=$password; $this->dev_acc=$dev_acc;
}
public function run($appname, $sd, $ed, $downloadFile) {
$this->loginAuth();
return $this->downloadReport($appname, $sd, $ed, $downloadFile);
}
private function loginAuth() {
$main = "https://play.google.com/apps/publish/";
$ck = GOOGLE_PLAY_COOKIE_FILE;
// Step 1: 取得初始 cookie
$ch = curl_init($main);
curl_setopt_array($ch, array(CURLOPT_COOKIEJAR=>$ck, CURLOPT_COOKIEFILE=>$ck, CURLOPT_RETURNTRANSFER=>1));
curl_exec($ch); curl_close($ch);
// Step 2: 取得 dsh, GALX
$loginUrl = "https://accounts.google.com/ServiceLogin?hl=en&continue=".urlencode($main);
$ch = curl_init($loginUrl);
curl_setopt_array($ch, array(CURLOPT_COOKIEJAR=>$ck, CURLOPT_COOKIEFILE=>$ck, CURLOPT_RETURNTRANSFER=>1));
$html = curl_exec($ch); curl_close($ch);
preg_match('/name="dsh"\s+id="dsh"\s+value="([^"]+)"/i', $html, $m); $dsh=$m[1];
preg_match('/name="GALX"\s+value="([^"]+)"/i', $html, $m); $galx=$m[1];
// Step 3: 提交登入
$post = "Email={$this->username}&Passwd={$this->password}&dsh=$dsh&GALX=$galx&signIn=Sign+in&PersistentCookie=yes&continue=".urlencode($main);
$ch = curl_init("https://accounts.google.com/ServiceLoginAuth");
curl_setopt_array($ch, array(
CURLOPT_POST=>1, CURLOPT_POSTFIELDS=>$post,
CURLOPT_COOKIEJAR=>$ck, CURLOPT_COOKIEFILE=>$ck,
CURLOPT_HEADER=>1, CURLOPT_FOLLOWLOCATION=>1, CURLOPT_RETURNTRANSFER=>1
));
curl_exec($ch); curl_close($ch);
}
private function downloadReport($package, $sd, $ed, $file) {
$dim = 'overall,country,language,os_version,device,app_version,carrier';
$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';
$url = "https://play.google.com/apps/publish/statistics/download?package=$package&sd=$sd&ed=$ed&dim=$dim&met=$met&dev_acc={$this->dev_acc}";
$fp = fopen($file, 'w');
$ch = curl_init($url);
curl_setopt_array($ch, array(CURLOPT_FILE=>$fp, CURLOPT_COOKIEFILE=>GOOGLE_PLAY_COOKIE_FILE));
curl_exec($ch); curl_close($ch);
fclose($fp);
return file_exists($file);
}
public function unzipReport($path, $file) {
exec("unzip $file -d $path");
unlink($file);
}
}
// 使用範例
$obj = new AndroidReportDownLoader('dev@gmail.com', 'password', '12345678901234567890');
if ($obj->run('com.example.app', '20230101', '20230131', 'report.zip')) {
$obj->unzipReport('/data/reports/', 'report.zip');
}
?>
dev_acc:開發者帳號 ID(可從後台 URL 觀察)。dim:維度,如 country、device、os_version。met:指標,官方文件列出 daily_device_installs、current_user_installs 等,需注意 Google 偶爾調整指標名稱。sd/ed:起迄日期 YYYYMMDD。完整程式碼可至本站下載。長期穩定方案建議遷移至 Google Play Developer API(RESTful、OAuth2、配額管理)。