Feasibility Study on Developing Android Applications with PHP
大家以前可能都听说过PFA(PHP for Android)这么一个东西,可以下结论的是,这个项目其实已经不再活跃失败了,因为其依赖的SL4A(Scripting Layer for Android)已经停止更新和维护了,而且它依赖SL4A的运行模式也跟我下面提到的PHPDroid的本地Browser/Server模式截然不同.
个人在Ubuntu上用交叉编译工具链musl-cross-compilers参考DroidPHP的教程构建了适用于Linux ARM架构的二进制PHP解释器.其中DroidPHP的教程用的是arm-none-linux-gnueabi那一套工具链,我发现不好使,后来我又看到了PocketMine,一个用PHP7构建的MineCraft Android版服务器端,它用的是musl-cross-compilers,所以我也改用这套工具链.我主要做了一些修改,比如修改libc库的shell路径,以及DNS相关的resolv.conf和hosts位置,主要是为了让PHP能在Android上跑.
现在有了能够运行在Android上的PHP解释器,接下来就是看看能用什么方法构建App.因为PHP解释器从5.4开始内置了一个CLI HTTP Server,这是一个C实现的支持PHP编程的单进程Web服务器,很轻量,官方定位是开发测试.而且它还支持一个特性,就是可以启动时给它指定一个PHP路由脚本:php -S 127.0.0.2:8080 -t /www auth.php
比如我打包的PHPDroid里的auth.php是这样的:
<code><?php //以下代码的意思就是,如果UserAgent不匹配,就输出Forbidden并退出 $ua = dirname(__FILE__).'/ua'; if( isset($_SERVER['HTTP_USER_AGENT']) && file_exists($ua) && $_SERVER['HTTP_USER_AGENT'] === trim(file_get_contents($ua)) ) { return false; } else { exit('Forbidden'); }</code>
我们还知道,PHP还内置了SQLite引擎,一个文件就是一个数据库,管理起来非常方便,可视化管理工具可以用免费开源跨平台的SQLiteStudio.现在有了HTTP服务器和SQLite数据库,我编译时还启用了一些常用的扩展,如下:
<code>bcmath calendar Core ctype curl date dom exif filter ftp gd hash json libxml mbstring mcrypt openssl pcre PDO pdo_sqlite Reflection session SimpleXML sockets SPL sqlite3 standard swoole xml xmlreader xmlwriter zip zlib</code>
有了上面这些东西,我们该怎么进行图形化交互呢?我想大家都想到了,那就是Android内置的WebView.个人认为,浏览器是有史以来最成功的跨平台GUI应用.Android从4.4开始,WebView底层由Chromium驱动,性能也是相当不错的.如果你要调用一些Java编写的本地功能,WebView一直都提供有一个addJavascriptInterface的特性,可以把Java对象注入到WebView中供JS调用,比如:
<code>Java代码: webview.addJavascriptInterface(new Object() { @JavascriptInterface public String getprop(String prop) { return java.lang.System.getProperties().getProperty(prop);; } }, "android"); WebView里的HTML代码: <a href="javascript:void(0)" onclick="alert(android.getprop('os.name'))">os.name</a></code>
也就是说,你用WebView也是可以进行一些本地API调用的,当然你得先用Java写好那些功能.
好了,现在PHPDroid的整个运行模式就比较清晰了,应用启动时,执行start.sh随机生成UserAgent并记录下来(ua.php),找到可用端口并记录下来(port.php),然后启动PHP内置HTTP服务器并记录PID(用于关闭),Java里启动WebView前读取随机生成UserAgent并setUserAgentString设置,PHP内置服务器的路由auth.php会对UserAgent进行比对,拒绝本机上其他应用(比如浏览器)发出的请求.
各个脚本如下:
<code>start.sh #!/system/bin/sh cd $1/php/bin chmod 700 busybox if [ ! -f php ]; then ./busybox xz -d file.tar.xz ./busybox tar xf file.tar && rm file.tar chmod 700 lib/ld php watcher qrencode ./busybox sed -i "s@/data/data/net.php.phpdroid@$1@g" php.ini ./busybox sed -i "s@/storage/self/primary@$2@g" php.ini cp resolv.conf $1/php/www cp hosts $1/php/www fi #随机生成UserAgent ./php -c php.ini ua.php #获取可用端口 ./php -c php.ini port.php #开发调试时把网站根目录复制到SD卡,方便修改 # -t $1/php/www 实际应用的网站根目录 # -t $2/phpdroid 开发调试的网站根目录 if [ ! -d $2/phpdroid ]; then cp -R $1/php/www $2/phpdroid; fi #启动PHP服务 $1/php/bin/php \ -c php.ini \ -S 127.0.0.2:`cat $1/php/bin/port` \ -t $2/phpdroid \ $1/php/bin/auth.php \ >/dev/null 2>&1 & #记录PHP的PID echo $! > pid #监听,发现文件auth.php被删除,则关闭PHP进程 $1/php/bin/watcher $1/php/bin/auth.php >/dev/null 2>&1 & #记录watcher的PID echo $! > pid_watcher return 0 stop.sh #!/system/bin/sh ua=$1/php/bin/ua if [ -e $ua ]; then rm $ua fi port=$1/php/bin/port if [ -e $port ]; then rm $port fi pid=$1/php/bin/pid if [ -e $pid ]; then kill -9 `cat $pid` rm $pid fi pid=$1/php/bin/pid_watcher if [ -e $pid ]; then kill -9 `cat $pid` rm $pid fi return 0 ua.php <?php $ua = 'Mozilla/5.0 (Linux; Android; PHPDroid) AppleWebKit (KHTML, like Gecko) Chrome Mobile Safari'; file_put_contents(dirname(__FILE__).'/ua', $ua.' '.sha1(uniqid(mt_rand(), true))); port.php <?php //PHP用 fsockopen 检测端口是否被占用,返回可用端口. $port = 8181; while ( $fp = @fsockopen('127.0.0.2', $port, $errno, $errstr, 1) ) { fclose($fp); $port++; } file_put_contents(dirname(__FILE__).'/port', $port); MainActivity.java 在 onCreate 时启动 WebView 如下: webview = new WebView(this); webview.getSettings().setUserAgentString(ua); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new WebChromeClient()); webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webview.loadUrl("http://127.0.0.2:" + port); setContentView(webview);</code>
好了,看到这里,大家基本已经明白我所说的用PHP开发Android应用的思路.
大家感兴趣可以下载我打包好的APK来体验一下:
phpdroid_20160703.apk(5.8M)
phpdroid_20160703.7z(4.7M)
apk里包含PHP最新的7.0.8和高性能网络编程扩展Swoole,
另外还有BusyBox和生成二维码的qrencode.
7z包是项目源代码,主要就是MainActivity.java和assets数据.
提示下,我打包的APK在start.sh里把网站根目录调整到了SD卡的phpdroid目录下,主要上方便大家体验时USB连接手机把自己写的PHP文件放到里面运行测试.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
