Home Backend Development PHP Tutorial 下载PHPDroid: 基于WebView和PHP内置HTTP服务器开发Android应用

下载PHPDroid: 基于WebView和PHP内置HTTP服务器开发Android应用

Jun 20, 2016 pm 12:32 PM

个人在Ubuntu上使用交叉编译工具链  arm-none-linux-gnueabi  按照  DroidPHP  的教程

构建了适用于Android(ARM架构)和树莓派Raspbian(ARM架构基于Debian的Linux发行版)的PHP解释器(cli,cli-server).

下载地址: http://pan.baidu.com/s/1gfJcXbX
内容包括:
main : 这个目录里面是项目源代码,主要就是MainActivity.java和assets数据.
phpdroid.apk: 应用示例,大小约7MB,包含PHP和BusyBox.

这里需要说明的是,BusyBox并不是PHP必备的东西,
打包它只是为了方便PHP能够调用里面常用的GNU/Linux命令,
去掉BusyBox后APK包还能缩小600KB.

需要强调的是,包里的PHP是路径无关的,运行也不需要root权限,
只要维持assets/php/的目录结构,放到你的应用里也能正常运行.
PHP的版本是5.5.9.

PHPDroid基本工作原理:
Java启动PHP内置的HTTP服务器,然后开一个WebView访问这个PHP驱动的HTTP服务.
其中,WebView用于实现人机交互,可以用传统的HTML/CSS/jQuery技术进行图形界面编程.
PHP则负责跟本地文件系统,SQLite数据库,网络进行交互.

需要强调的是,PHPDroid追求的不是像Java App那样能够访问Android系统提供的API.
PHPDroid的优势在于用传统的Web开发技术HTML/CSS/JS/PHP/SQL就能开发基于WebView的本地WebApp.
PHPDroid内置的本地PHP不能访问Android提供给Java的API,但可以操作本地文件系统(应用目录)和SQLite以及进行网络交互.
比如获取一个新闻列表,WebView通过AJAX访问本地PHP,PHP再通过cURL等访问远程服务器.
远程服务器返回JSON,里面包含新闻的标题,摘要,缩略图网址,本地PHP转成数组后循环输出到WebView.
可见这个本地PHP既是WebView的服务器端,又是远程服务器的客户端,是WebView和远程服务器数据交互的中转站.
把WebView和本地PHP看做一个整体,那它就是一个不能调用Android API的本地WebApp.
毕竟Android是Linux内核,一切皆文件的思想还是在那里的,
只要有权限,PHP读取一些系统数据(比如/proc/cpuinfo)并没有问题.

PHPDroid详细工作原理:
phpdroid/app/src/main/java/net/php/phpdroid/MainActivity.java
MainActivity在onCreate首次启动时复制:
/data/app/net.php.phpdroid.apk/assets/php/
到:
/data/data/net.php.phpdroid/php/

然后Runtime.getRuntime().exec执行PHP服务启动脚本:
/data/data/net.php.phpdroid/php/bin/start.sh
#!/system/bin/sh
chmod 700 $1/php/bin/busybox
chmod 700 $1/php/bin/php
$1/php/bin/php $1/php/bin/ua.php #随机生成UserAgent
$1/php/bin/php $1/php/bin/port.php #获取可用端口
$1/php/bin/php \
-c $1/php/bin/php.ini \
-d app_dir="$1" \
-d upload_tmp_dir="$1/php/tmp" \
-d session.save_path="$1/php/tmp" \
-S 127.0.0.2:`cat $1/php/bin/port` \
-t $1/php/www \
$1/php/bin/auth.php \
>/dev/null 2>&1 &
echo $! > $1/php/bin/pid #记录PHP的PID
return 0
这个脚本的作用就是,随机生成用于标记WebView的UserAgent,获取127.0.0.2上的可用端口,
然后启动PHP服务器,记录其PID,用于在kill关闭.
关于PHP内置HTTP服务器的介绍,请看:
https://wiki.php.net/rfc/builtinwebserver

其中:
/data/data/net.php.phpdroid/php/bin/ua.php
file_put_contents(dirname(__FILE__).'/ua', sha1(uniqid(mt_rand(), true)));

/data/data/net.php.phpdroid/php/bin/port.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);

/data/data/net.php.phpdroid/php/bin/auth.php
//触发PHP服务进程打开resolv_php.conf,要求resolv_php.conf跟auth.php同在一个目录
gethostbyname('localhost');
$ua = file_get_contents(dirname(__FILE__).'/ua');
if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']===$ua) {
return false;
} else {
exit('Auth Failed');
}
PHP服务在处理每个请求之前,都会执行auth.php文件,
如果ua(UserAgent)不匹配,程序就会exit退出.
Android上一个应用对应一个用户,每个应用目录只允许应用所属用户进行访问,
所以除非手机被root,否则其他应用是没法读取PHPDroid应用目录里的数据的.
应用MainActivity.java里读取ua文件并设置为WebView的UserAgent,所以能够访问PHP服务.
手机上的其他应用,比如浏览器,因为没有读取其他应用目录比如 /data/data/net.php.phpdroid 的权限,
也就无法读取PHPDroid生成的ua,自然也就无法访问PHP服务.
MainActivity.java
webview.getSettings().setUserAgentString(ua);
webview.loadUrl("http://127.0.0.2:" + port);

关于DNS解析,glibc默认访问的是/etc/resolv.conf
#define _PATH_RESCONF "/etc/resolv.conf"
在编译glibc时,我改成了相对路径:
#define _PATH_RESCONF "./resolv_php.conf"

/data/data/net.php.phpdroid/php/bin/resolv_php.conf
# 百度公共DNS http://dudns.baidu.com/
nameserver 180.76.76.76
# CNNIC公共DNS http://www.sdns.cn/
nameserver 1.2.4.8
nameserver 210.2.4.8
静态链接了glibc库的PHP,在执行auth.php里的gethostbyname('localhost')操作时,
会触发访问auth.php所在目录下的resolv_php.conf,从而进行DNS.

更好的方法应该是调用Android的getprop net.dns1获取本地DNS,然后加入到resolv_php.conf里.
但奇怪的是,在adb shell里执行 getprop net.dns1 能正确输出,
一套在PHP的 echo shell_exec('getprop net.dns1'); 就没有输出了.
执行 echo shell_exec('vmstat'); 调用其他命令是能正常输出的.
这里不知道是什么问题,权限问题么?请高人指点.

关于glibc的编译,我还把调用命令的/bin/sh改成了Android的/system/bin/sh,
这样PHP的shell_exec等函数才能正常运行.
sed -i "s{/bin/sh{/system/bin/sh{" ./libio/oldiopopen.c
sed -i "s{/bin/sh{/system/bin/sh{" ./libio/iopopen.c
sed -i "s{/bin/sh{/system/bin/sh{" ./posix/tst-vfork3.c
sed -i "s{/bin/sh{/system/bin/sh{" ./posix/bug-regex9.c
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/posix/system.c
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/generic/paths.h
sed -i "s{/bin/sh{/system/bin/sh{" ./sysdeps/unix/sysv/linux/paths.h
位于PHP里的proc_open函数也要进行类似修改:
sed -i "s{/bin/sh{/system/bin/sh{" ext/standard/proc_open.c
这样PHP就可以愉快地调用Android和BusyBox里提供的GNU/Linux常用命令了.


MainActivity在onKeyDown按下返回键KEYCODE_BACK退出应用时:
会调用stop.sh关闭PHP服务,stop.sh内容如下:
#!/system/bin/sh
ua=$1/php/bin/ua
if [ -r $ua ]; then
rm $ua
fi
port=$1/php/bin/port
if [ -r $port ]; then
rm $port
fi
pid=$1/php/bin/pid
if [ -r $pid ]; then
kill `cat $pid`
rm $pid
fi
return 0
就是把ua,port这两个文件删掉,并且关闭PHP进程.
其实MainActivity在启动时也会调用stop.sh清理上次应用可能意外退出遗留下来的东西.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles