PHP apk解包识版本号信息和ipa包信息

阅读 (6231)
去年做的一个项目,公司需要一个app包管理平台(公司APP比较多),用来上传管理不同app不同版本的APP,但又不想手动填包信息,所以就需要一个自动识别的功能。

1. apk解包插件

tufanbarisyildirim/php-apk-parser

#安装

composer install tufanbarisyildirim/php-apk-parser

#使用

// 识别APK信息
$apk = new \ApkParser\Parser($this->targetFile);
$manifest = $apk->getManifest();

// 包名
$this->package_name = $manifest->getPackageName();

// 版本号
$this->version_name = $manifest->getVersionName();

// 版本编号
$this->version_code = $manifest->getVersionCode();

// 其他方法参考官方文档

 

2. ipa 解包 需要两个插件

解压缩插件,解压过后再识别plist文件内容,找到包信息

chumper/zipper

#安装

compose install chumper/zipper

plist文件识别

rodneyrehm/plist

#安装

compose install rodneyrehm/plist

使用

// 遍历zip包中的Info.plist文件
        $zipFiles = Zipper::make($this->targetFile)->listFiles('/Info\.plist$/i');
        
        $matched = 0;
        if ($zipFiles) {
            foreach ($zipFiles as $k => $filePath) {
                // 正则匹配包根目录中的Info.plist文件
                if (preg_match("/Payload\/([^\/]*)\/Info\.plist$/i", $filePath, $matches)) {
                    $matched = 1;

                    $this->app_folder = $matches[1];

                    // 将plist文件解压到ipa目录中的对应包名目录中
                    Zipper::make($this->targetFile)->folder('Payload/'.$this->app_folder)->extractMatchingRegex(storage_path('app/public/'.env('APP_ENV', 'local').'/upload/plist/'.$this->app_folder), "/Info\.plist$/i");

                    // 拼接plist文件完整路径
                    $fp = storage_path('app/public/'.env('APP_ENV', 'local').'/upload/plist/'.$this->app_folder.'/Info.plist');
                    
                    // 获取plist文件内容
                    $content = file_get_contents($fp);

                    // 解析plist成数组
                    $ipa = new \CFPropertyList\CFPropertyList();
                    $ipa->parse($content);
                    $ipaInfo = $ipa->toArray();

                    // ipa 解包信息
                    $this->ipa_data_bak = json_encode($ipaInfo);

                    // 包名
                    $this->package_name = $ipaInfo['CFBundleIdentifier'];
                    
                    // 版本名
                    $this->version_name = $ipaInfo['CFBundleShortVersionString'];

                    // 版本号
                    $this->version_code = str_replace('.', '', $ipaInfo['CFBundleShortVersionString']);

                    // 别名
                    $this->bundle_name = $ipaInfo['CFBundleName'];

                    // 显示名称
                    $this->display_name =  $ipaInfo['CFBundleDisplayName'];
                }
            }

        }
更新于:2018-06-01 15:08:53
返回顶部