


How to get apk package information in php, get apk package in php
Sometimes when using php to upload an Android apk package, we need to obtain the information in the Android apk package. This article describes the method of obtaining apk package information in php in the form of examples. The specific implementation method is as follows:
<?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(); // 版本代码 ?>
The following is the Apkparser class package. Copy the following code and save it as Apkparser.php to execute the above code
<?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; 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' => '<?xml version="1.0" encoding="utf-8"?>' ); 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'] = "</{$props['ns_name']}>"; 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类包结束 //--------------------- ?>
Interested friends can debug and run the example in this article. I believe it will bring some inspiration to everyone's PHP program development.
$_POST is an array, echo $_POST is of course Array. You can var_dump($_POST) to see the parameters and values in the array. If the parameter you passed is a and the value is hello, you can use $_POST['a'] to get it, imitating the GET method welcome.php?a=hello
echo $_GET['a'];
Your current approach is the only feasible approach. You can only refresh the APK regularly. Unless the APK and the server remain connected, the server's PHP is the service to find the APK, because your APK is not a server and cannot be executed in reverse.
If you want to reduce the pressure on the server, you must make your APK a server, listen on a network port, and allow other devices on the network to connect. However, this project will be very large, and the logic of APK and PHP will become very complicated, because PHP needs to find APK when needed. If the phone is turned off, it will have to wait for a while, and APK needs to use services in order to wait for PHP to connect at any time. Work the way you want and be on call at any time.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
