设为首页 加入收藏

TOP

PHP获取APK的包信息(一)
2017-10-10 12:01:08 】 浏览:2252
Tags:PHP 获取 APK 信息

这段时间太忙了,一个月没有写博客了,稍微闲下来就感觉把在开发中遇到的问题记录下来

php上传安卓apk包的时候,需要获取安卓apk包内的信息

<?php

/*解析安卓apk包中的压缩XML文件,还原和读取XML内容

依赖功能:需要PHP的ZIP包函数支持。*/

include('./Apkparser.php');

$appObj    = new Apkparser(); 

$targetFile = a.apk;//apk所在的路径地址

$res     = $appObj->open($targetFile);

$appObj->getAppName();         // 应用名称

$appObj->getPackage();        // 应用包名

$appObj->getVersionName();    // 版本名称

$appObj->getVersionCode();    // 版本代码
?>

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

<?php
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->getAttributeva lue($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 U
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇初学者对WAMP服务器的设置 下一篇手机safari图片上传竖变横处理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目