简体   繁体   English

如何在 WebView 应用程序中本地运行 PHP 代码?

[英]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.所以我收到了一个严格限制的项目,我需要为 WebView 应用程序运行存储在本地应用程序目录中的 php 文件。 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.我在google play看到以下app,声称有php server、mysql等。

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

So I know there is possibility to run php files within android application.所以我知道有可能在 android 应用程序中运行 php 文件。

What my idea is,我的想法是,

  • Have the php server in a webview application, Add php files to an app directory,在 webview 应用程序中拥有 php 服务器,将 php 文件添加到应用程序目录,
  • Configure webview to open the following URL "Localhost"配置 webview 以打开以下 URL “Localhost”
  • Webview application does not need requirement of MySQL, just simply process php. Webview 应用程序不需要 MySQL,只需简单的处理 php。
  • Webview application does not need to worry about storage because no storage will be saved on it. Webview 应用程序不需要担心存储,因为它不会保存任何存储。

What I am NOT seeking for in this question我在这个问题中不寻求什么

  • Using other apps in playstore, because this will be an independent application.在 Playstore 中使用其他应用程序,因为这将是一个独立的应用程序。
  • Being instructed to convert the files into other formats such as html, javascript, etc.被指示将文件转换为其他格式,例如 html、javascript 等。
  • suggesting options not relevant to the question.建议与问题无关的选项。

I am aware php is server sided code.我知道 php 是服务器端代码。

This is a list of challenges you face if you wish to run PHP code on a modern Android device.如果您希望在现代 Android 设备上运行 PHP 代码,这是您面临的挑战列表。

I searched and found some projects implying that PHP can be run on Android我搜索了一些项目,发现一些项目暗示 PHP 可以在 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 .虽然我不能谈论任何闭源项目,但有一些开源项目, DroidPHP和更新的MyServerlampTermux But a bit of history/background is needed to understand what these projects represent.但是需要一些历史/背景来理解这些项目代表什么。

PHP is written in C/C++. PHP 是用 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 .虽然 Android 确实通过 Android NDK 支持 C/C++,但缺少的是 PHP 服务器期望在Android 不支持开箱即用的恒定功率Posix 兼容环境中运行。

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 .第一个DroidPHP似乎被作者抛弃了,它使用Busybox ,它提供了一个 POSIX 兼容的环境,但需要超级用户/root 权限,这需要“root”Android 设备 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.多年来,Android 生态系统拥有数十亿用户,提高了每个版本的安全性,减少了任何不良行为者的攻击面。

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.其他两个 github 项目使用Termux ,它提供了一个类似 POSIX 的环境,可以与 root 一起使用,并且在没有 root 的情况下以更有限的方式使用。

Hey that sounds great, so I can use Termux to run my PHP code.嘿,听起来不错,所以我可以使用Termux来运行我的 PHP 代码。

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. Android 制造商已经添加了他们自己的省电处理,从而产生了一个类似的网站: https ://dontkillmyapp.com/ 如果您的应用只在前台运行,那么您可能是安全的。

  2. From the Termux repo README regarding Android 12:来自关于 Android 12 的 Termux 存储库自述文件:

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.注意: Termux 在 Android 12 上已损坏。Android 操作系统将终止任何大于 32 的(幻像)进程(限制适用于所有应用程序的组合),并且还会终止任何使用过多 CPU 的进程。

  1. You'll need to host your own packages, if you intend to release a standalone app.如果您打算发布独立应用程序,则需要托管自己的包。 From the Termux FAQ来自Termux 常见问题

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.是的,因为 Termux 是开源的并且使用 GNU GPL v3.0 并且在某些组件中使用 Apache-2.0 许可证,所以只要满足许可证要求,您就可以自由地重用现有组件。

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 .开始使用开发人员 wiki 页面上的信息: https ://github.com/termux/termux-packages/wiki。

  1. Due to increased security restrictions Termux is no longer supported on the Google Play Store .由于增加了安全限制,Google Play 商店不再支持 Termux 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.因此,如果 Android 应用程序用于商业发布,那么将您的项目迁移到 Java/Kotlin 甚至 WebView 中的 JavaScript 将允许 Google Play 商店提供支持,因为Termux (及其操作要求)当前不兼容。

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/资料来源: https ://www.reddit.com/r/termux/comments/p3exkr/how_can_i_make_an_android_apk_app_based_on_termux/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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