简体   繁体   中英

How can I run PHP Code locally within a WebView App?

So I have been issued a project with tight restrictions, I need to run php files stored in local app directory for a WebView application. However I dont even know where to start as for implementing or integrating

I see the following apps in google play, claiming to have php server, mysql, etc.

https://play.google.com/store/apps/details?id=com.sylkat.apache

So I know there is possibility to run php files within android application.

What my idea is,

  • Have the php server in a webview application, Add php files to an app directory,
  • Configure webview to open the following URL "Localhost"
  • Webview application does not need requirement of MySQL, just simply process php.
  • Webview application does not need to worry about storage because no storage will be saved on it.

What I am NOT seeking for in this question

  • Using other apps in playstore, because this will be an independent application.
  • Being instructed to convert the files into other formats such as html, javascript, etc.
  • suggesting options not relevant to the question.

I am aware php is server sided code.

This is a list of challenges you face if you wish to run PHP code on a modern Android device.

I searched and found some projects implying that PHP can be run on Android

While I can't speak to any closed source projects, there are a few open source one, DroidPHP and more recent ones MyServer and lampTermux . But a bit of history/background is needed to understand what these projects represent.

PHP is written in C/C++. While Android does support C/C++ via the Android NDK, what's missing is that a PHP server expects to be run in a constant power Posix compliant environment which Android does not support out of the box .

The first one DroidPHP , which appears abandoned by the author, uses Busybox which provides a POSIX compatible environment but requires superuser/root permission which requires 'rooting' the Android device . Note that 'rooting' generally voids device warranties.

With billions of users over the years, the Android ecosystem has increased thesecurity of each version reducing the attack surface for any bad-actor.

The other two github projects use Termux which provide a POSIX-like environment, which can be used with root and in a more limited fashion without root.

Hey that sounds great, so I can use Termux to run my PHP code.

A few caveats:

  1. Android manufacturers have added in their own battery saving processing resulting in a site like: https://dontkillmyapp.com/ if your app only runs in foreground you are probably safe.

  2. From the Termux repo README regarding Android 12:

NOTICE: Termux is broken on Android 12. Android OS will kill any (phantom) processes greater than 32 (limit is for all apps combined) and also kill any processes using excessive CPU.

  1. You'll need to host your own packages, if you intend to release a standalone app. From the Termux FAQ

Yes, as Termux is open-source and uses a GNU GPL v3.0 and in some components Apache-2.0 license, you can freely re-use existing components as soon as you met the license requirements.

However license does not cover "free" use of our hosting capabilities which are somewhat limited. You cannot use our package repositories in your own project(s). Please build packages and host them yourself.

Get started with information on developer's wiki pages: https://github.com/termux/termux-packages/wiki .

  1. Due to increased security restrictions Termux is no longer supported on the Google Play Store . So if the Android app is for commercial release, migrating your project to Java/Kotlin or even JavaScript in a WebView will allow for support in the Google Play Store, given that Termux (and its requirements for operation) is currently incompatible.

If you follow the embedded attempt issue thread you'll see that embedding is a involved process.

Sources: https://www.reddit.com/r/termux/comments/p3exkr/how_can_i_make_an_android_apk_app_based_on_termux/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM