php获取apk包信息的类

这篇博客分享了一个用于获取安卓APK包信息的PHP类。由于JavaScript无法直接解析ZIP格式的APK,作者选择了在服务器端使用PHP进行处理。通过上传APK到服务器,然后利用PHP代码解析APK,获取其中的详细信息。代码简洁易用,适合有类似需求的开发者参考。
摘要由CSDN通过智能技术生成

最近由于公司的需求,需要为游戏的开放平台做一个上传apk的功能其中需要获取包的一些信息,于是在网上扒来了一个获取apk信息的操作类,觉得写的挺好的,所以写下来和大家分享,同时记录一下。

由于js无法打开zip包,所以无法通过js来解析apk获取其中的信息,所以我们只能先将包上传到服务器,再用php来操作他(当然java于是可以滴),话补多说,先上代码:

<?php
//-------------------------------
//Apkparser类包开始
//-------------------------------
class ApkParser{
   
//----------------------
// 公共函数,供外部调用
//----------------------
  public function open($apk_file, $xml_file='AndroidManifest.xml'){
   
    $zip = new \ZipArchive;
    if ($zip->open($apk_file) === TRUE) {
      $xml = $zip->getFromName($xml_file);
      $zip->close();
      if ($xml){
        try {
          return $this->parseString($xml);
        }catch (Exception $e){
        }
      }
    }
    return false;
  }
  public function parseString($xml){
   
    $this->xml = $xml;
    $this->length = strlen($xml);
    $this->root = $this->parseBlock(self::AXML_FILE);
    return true;
  }
  public function getXML($node=NULL, $lv=-1){
   
    if ($lv == -1) $node = $this->root;
    if (!$node) return '';
    if ($node['type'] == self::END_TAG) $lv--;
    $xml = @($node['line'] == 0 || $node['line'] == $this->line) ? '' : "\n".str_repeat(' ', $lv);
    $xml .= $node['tag'];
    $this->line = @$node['line'];
    foreach ($node['child'] as $c){
      $xml .= $this->getXML($c, $lv+1);
    }
    return $xml;
  }
  public function getPackage(){
   
    return $this->getAttribute('manifest', 'package');
  }
  public function getVersionName(){
   
    return $this->getAttribute('manifest', 'android:versionName');
  }
  public function getVersionCode(){
   
    return $this->getAttribute('manifest', 'android:versionCode');
  }
  public function getAppName(){
   
    return $this->getAttribute('manifest/application', 'android:name');
  }
  public function getMainActivity(){
   
    for ($id=0; true; $id++){
      $act = $this->getAttribute("manifest/application/activity[{$id}]/intent-filter/action", 'android:name');
      if (!$act) break;
      if ($act == 'android.intent.action.MAIN') return $this->getActivity($id);
    }
    return NULL;
  }
  public function getActivity($idx=0){
   
    $idx = intval($idx);
    return $this->getAttribute("manifest/application/activity[{$idx}]", 'android:name');
  }
  public function getAttribute($path, $name){
   
    $r = $this->getElement($path);
    if (is_null($r)) return NULL;
    if (isset($r['attrs'])){
      foreach ($r['attrs'] as $a){
        if ($a['ns_name'] == $name) return $this->getAttributeValue($a);
      }
    }
    return NULL;
  }
//----------------------
// 类型常量定义
//----------------------
  const AXML_FILE       = 0x00080003;
  const STRING_BLOCK     = 0x001C0001;
  const RESOURCEIDS      = 0x00080180;
  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值