侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php获取apk包信息的方法

php  /  管理员 发布于 7年前   161

有时候在使用php上传安卓apk包的时候,我们需要获取安卓apk包内的信息,本文以实例形式讲述了php获取apk包信息的方法。具体实现方法如下:

open($targetFile);$appObj->getAppName();     // 应用名称$appObj->getPackage();    // 应用包名$appObj->getVersionName();  // 版本名称$appObj->getVersionCode();  // 版本代码?>

以下是Apkparser类包,把以下代码复制出来保存为Apkparser.php就可以执行以上代码

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;  const START_NAMESPACE    = 0x00100100;  const END_NAMESPACE     = 0x00100101;  const START_TAG       = 0x00100102;  const END_TAG        = 0x00100103;  const TEXT         = 0x00100104;  const TYPE_NULL       =0;  const TYPE_REFERENCE    =1;  const TYPE_ATTRIBUTE    =2;  const TYPE_STRING      =3;  const TYPE_FLOAT      =4;  const TYPE_DIMENSION    =5;  const TYPE_FRACTION     =6;  const TYPE_INT_DEC     =16;  const TYPE_INT_HEX     =17;  const TYPE_INT_BOOLEAN   =18;  const TYPE_INT_COLOR_ARGB8 =28;  const TYPE_INT_COLOR_RGB8  =29;  const TYPE_INT_COLOR_ARGB4 =30;  const TYPE_INT_COLOR_RGB4  =31;  const UNIT_MASK       = 15;  private static $RADIX_MULTS = array(0.00390625, 3.051758E-005, 1.192093E-007, 4.656613E-010);  private static $DIMENSION_UNITS = array("px","dip","sp","pt","in","mm","","");  private static $FRACTION_UNITS = array("%","%p","","","","","","");  private $xml='';  private $length = 0;  private $stringCount = 0;  private $styleCount = 0;  private $stringTab = array();  private $styleTab = array();  private $resourceIDs = array();  private $ns = array();  private $cur_ns = NULL;  private $root = NULL;  private $line = 0;//----------------------// 内部私有函数//----------------------  private function getElement($path){    if (!$this->root) return NULL;    $ps = explode('/', $path);    $r = $this->root;    foreach ($ps as $v){      if (preg_match('/([^\[]+)\[([0-9]+)\]$/', $v, $ms)){        $v = $ms[1];        $off = $ms[2];      }else {        $off = 0;      }      foreach ($r['child'] as $c){        if ($c['type'] == self::START_TAG && $c['ns_name'] == $v){          if ($off == 0){$r = $c; continue 2;          }else {$off--;          }        }      }      // 没有找到节点      return NULL;    }    return $r;  }  private function parseBlock($need = 0){    $o = 0;    $type = $this->get32($o);    if ($need && $type != $need) throw new Exception('Block Type Error', 1);    $size = $this->get32($o);    if ($size < 8 || $size > $this->length) throw new Exception('Block Size Error', 2);    $left = $this->length - $size;    $props = false;    switch ($type){      case self::AXML_FILE:        $props = array(          'line' => 0,          'tag' => ''        );      break;      case self::STRING_BLOCK:        $this->stringCount = $this->get32($o);        $this->styleCount = $this->get32($o);        $o += 4;        $strOffset = $this->get32($o);        $styOffset = $this->get32($o);        $strListOffset = $this->get32array($o, $this->stringCount);        $styListOffset = $this->get32array($o, $this->styleCount);        $this->stringTab = $this->stringCount > 0 ? $this->getStringTab($strOffset, $strListOffset) : array();        $this->styleTab = $this->styleCount > 0 ? $this->getStringTab($styOffset, $styListOffset) : array();        $o = $size;      break;      case self::RESOURCEIDS:        $count = $size / 4 - 2;        $this->resourceIDs = $this->get32array($o, $count);      break;      case self::START_NAMESPACE:        $o += 8;        $prefix = $this->get32($o);        $uri = $this->get32($o);        if (empty($this->cur_ns)){          $this->cur_ns = array();          $this->ns[] = &$this->cur_ns;        }        $this->cur_ns[$uri] = $prefix;      break;      case self::END_NAMESPACE:        $o += 8;        $prefix = $this->get32($o);        $uri = $this->get32($o);        if (empty($this->cur_ns)) break;        unset($this->cur_ns[$uri]);      break;      case self::START_TAG:        $line = $this->get32($o);        $o += 4;        $attrs = array();        $props = array(          'line' => $line,          'ns' => $this->getNameSpace($this->get32($o)),          'name' => $this->getString($this->get32($o)),          'flag' => $this->get32($o),          'count' => $this->get16($o),          'id' => $this->get16($o)-1,          'class' => $this->get16($o)-1,          'style' => $this->get16($o)-1,          'attrs' => &$attrs        );        $props['ns_name'] = $props['ns'].$props['name'];        for ($i=0; $i < $props['count']; $i++){          $a = array('ns' => $this->getNameSpace($this->get32($o)),'name' => $this->getString($this->get32($o)),'val_str' => $this->get32($o),'val_type' => $this->get32($o),'val_data' => $this->get32($o)          );          $a['ns_name'] = $a['ns'].$a['name'];          $a['val_type'] >>= 24;          $attrs[] = $a;        }        // 处理TAG字符串        $tag = "<{$props['ns_name']}";        foreach ($this->cur_ns as $uri => $prefix){          $uri = $this->getString($uri);          $prefix = $this->getString($prefix);          $tag .= " xmlns:{$prefix}=\"{$uri}\"";        }        foreach ($props['attrs'] as $a){          $tag .= " {$a['ns_name']}=\"".  $this->getAttributeValue($a).  '"';        }        $tag .= '>';        $props['tag'] = $tag;        unset($this->cur_ns);        $this->cur_ns = array();        $this->ns[] = &$this->cur_ns;        $left = -1;      break;      case self::END_TAG:        $line = $this->get32($o);        $o += 4;        $props = array(          'line' => $line,          'ns' => $this->getNameSpace($this->get32($o)),          'name' => $this->getString($this->get32($o))        );        $props['ns_name'] = $props['ns'].$props['name'];        $props['tag'] = "";        if (count($this->ns) > 1){          array_pop($this->ns);          unset($this->cur_ns);          $this->cur_ns = array_pop($this->ns);          $this->ns[] = &$this->cur_ns;        }      break;      case self::TEXT:        $o += 8;        $props = array(          'tag' => $this->getString($this->get32($o))        );        $o += 8;      break;      default:        throw new Exception('Block Type Error', 3);      break;    }    $this->skip($o);    $child = array();    while ($this->length > $left){      $c = $this->parseBlock();      if ($props && $c) $child[] = $c;      if ($left == -1 && $c['type'] == self::END_TAG){        $left = $this->length;        break;      }    }    if ($this->length != $left) throw new Exception('Block Overflow Error', 4);    if ($props){      $props['type'] = $type;      $props['size'] = $size;      $props['child'] = $child;      return $props;    }else {      return false;    }  }  private function getAttributeValue($a){    $type = &$a['val_type'];    $data = &$a['val_data'];    switch ($type){      case self::TYPE_STRING:        return $this->getString($a['val_str']);      case self::TYPE_ATTRIBUTE:        return sprintf('?%s%08X', self::_getPackage($data), $data);      case self::TYPE_REFERENCE:        return sprintf('@%s%08X', self::_getPackage($data), $data);      case self::TYPE_INT_HEX:        return sprintf('0x%08X', $data);      case self::TYPE_INT_BOOLEAN:        return ($data != 0 ? 'true' : 'false');      case self::TYPE_INT_COLOR_ARGB8:      case self::TYPE_INT_COLOR_RGB8:      case self::TYPE_INT_COLOR_ARGB4:      case self::TYPE_INT_COLOR_RGB4:        return sprintf('#%08X', $data);      case self::TYPE_DIMENSION:        return $this->_complexToFloat($data).self::$DIMENSION_UNITS[$data & self::UNIT_MASK];      case self::TYPE_FRACTION:        return $this->_complexToFloat($data).self::$FRACTION_UNITS[$data & self::UNIT_MASK];      case self::TYPE_FLOAT:        return $this->_int2float($data);    }    if ($type >=self::TYPE_INT_DEC && $type < self::TYPE_INT_COLOR_ARGB8){      return (string)$data;    }    return sprintf('<0x%X, type 0x%02X>', $data, $type);  }  private function _complexToFloat($data){    return (float)($data & 0xFFFFFF00) * self::$RADIX_MULTS[($data>>4) & 3];  }  private function _int2float($v) {    $x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);    $exp = ($v >> 23 & 0xFF) - 127;    return $x * pow(2, $exp - 23);  }  private static function _getPackage($data){    return ($data >> 24 == 1) ? 'android:' : '';  }  private function getStringTab($base, $list){    $tab = array();    foreach ($list as $off){      $off += $base;      $len = $this->get16($off);      $mask = ($len >> 0x8) & 0xFF;      $len = $len & 0xFF;      if ($len == $mask){        if ($off + $len > $this->length) throw new Exception('String Table Overflow', 11);        $tab[] = substr($this->xml, $off, $len);      }else {        if ($off + $len * 2 > $this->length) throw new Exception('String Table Overflow', 11);        $str = substr($this->xml, $off, $len * 2);        $tab[] = mb_convert_encoding($str, 'UTF-8', 'UCS-2LE');      }    }    return $tab;  }  private function getString($id){    if ($id > -1 && $id < $this->stringCount){      return $this->stringTab[$id];    }else {      return '';    }  }  private function getNameSpace($uri){    for ($i=count($this->ns); $i > 0; ){      $ns = $this->ns[--$i];      if (isset($ns[$uri])){        $ns = $this->getString($ns[$uri]);        if (!empty($ns)) $ns .= ':';        return $ns;      }    }    return '';  }  private function get32(&$off){    $int = unpack('V', substr($this->xml, $off, 4));    $off += 4;    return array_shift($int);  }  private function get32array(&$off, $size){    if ($size <= 0) return NULL;    $arr = unpack('V*', substr($this->xml, $off, 4 * $size));    if (count($arr) != $size) throw new Exception('Array Size Error', 10);    $off += 4 * $size;    return $arr;  }  private function get16(&$off){    $int = unpack('v', substr($this->xml, $off, 2));    $off += 2;    return array_shift($int);  }  private function skip($size){    $this->xml = substr($this->xml, $size);    $this->length -= $size;  }}//---------------------//Apkparser类包结束 //---------------------?>

感兴趣的朋友可以调试运行一下本文实例,相信会对大家的php程序开发带来一定的启发。

您可能感兴趣的文章:

  • 查看apk签名信息方法
  • jarsigner重新签名apk无法安装的解决方法
  • Android中获取apk安装包信息的方法
  • unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
  • 教你如何使用platform密钥对apk进行签名
  • 使用python编写脚本获取手机当前应用apk的信息
  • Android实现检查并下载APK更新、安装APK及获取网络信息的方法
  • 使用android-apktool来逆向(反编译)APK包方法介绍
  • Android快速分析apk工具aapt的使用教程
  • java获取手机已安装APK的签名摘要


  • 上一条:
    PHP反向代理类代码
    下一条:
    PHP解码unicode编码的中文字符代码分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客