What is Android Termux?

What is PHP Termux API? Termux Tutorial on How to Use PHP

Termux Android Tutorial: Accessing Android Device Features using the Termux API

Termux PHP Tutorial: Steps to Have the Full Setup for Using Termux PHP

Accessing the device using a keyboard

Termux FTP Files Access

Running PHP from Termux in an endless loop

Control Android Tablet with Phone via Web Server

Running as Android Server for PHP Local Web Applications

Shall I Use Termux lighttpd, Apache, nginx or PHP built-in Web Server?

Using the PHP Termux API class

What is Android Termux?

Termux App is a useful Android app which enables us to use a sort of Linux console inside our Android devices, like phones, tablets, and so on.

It needs the Termux:API add-on app to be installed, so it can use a command line programs that expose Android functions to Termux system. Inside Termux we can access networking (wi-fi and mobile internet).

What is PHP Termux API? Termux Tutorial on How to Use PHP

Termux supports package management using .deb files and the apt tool. In his own package repository there are many tools like imagemagick, a c language compiler, PHP 7.1 interpreter, lighttpd, etcetera. The actual build supports only ARM and x86 platforms, so can’t install on MIPS.

So now we have PHP 7.1 on the smartphone too… ready to begin the development of new applications? We need to access some of the Android features to power up our projects. the Termux:API add some command line tools to deal; a wrapper class is the only we need to take advantage of it.

Termux Android Tutorial: Accessing Android Device Features using the Termux API

The PHP Termux API package provides methods to access the following functionality exposed bt the Termux API app.

Termux PHP Tutorial: Steps to Have the Full Setup for Using Termux PHP

Accessing the device using a keyboard

We need to type in a terminal emulator capable and requiring of ctrl-c, so I recommend to install a keyboard with it (as Hacker’s keyboard does).

Termux FTP Files Access

To transfer PHP scripts and other files easily between our workstation and the Android devices, a FTP server is a must.

It is easy to set up your desktop environment to use LAN FTP servers. You can install a FTP client in the system, or even use inside Mozilla Firefox (FireFTP extension). In Windows, type in the Explorer Address Bar, the URL of the server, like:

ftp://[email protected]:port/

The same can be done for other operating systems desktop environments, like Nautilus for Linux, etc..

Running PHP from Termux in an endless loop

For polling endlessly some conditions. Inside the script, the loop must have a sleep(seconds) call, to avoid monopolize the processor.

in Termux, type:

Control Android Tablet with Phone via Web Server

One way to control a tablet or other type of mobile device from a phone is to run a Web server on the tablet device and access to it via a browser running on the phone.

If you run PHP on the tablet device as a Web server with Termux, it will be very easy to program a simple application that can do whatever you want to control the tablet.

Running as Android Server for PHP Local Web Applications

You can run PHP and make it work as a Web server that executes your PHP scripts in side your project root directory stored in your device memory file system.

In Termux, type In order to start the development Web server embedded in PHP.

If you use an address that is 0.0.0.0, the server listens to any of the device network IP addresses. if address is 127.0.0.1, the server only listens to local connections (inside the device or phone).

The port must be any number over 1024. the rootdirectory is the base path which serve as root web directory, with all the resources including JavaScript, images, CSS stylesheets, HTML files, and PHP scripts.

Shall I Use Termux lighttpd, Apache, NGINX or PHP built-in Web Server?

Usually PHP is used in production environment along with other Web servers like lighttpd, Apache or NGINX because those Web servers have been optimized to deal with high loads.

However on a mobile phone you will only have your own user accessing the Web server. Therefore, the built-in PHP Web server is good enough for this purpose, unless you need some specific Web server features that those high performance Web servers provide.

Using the PHP Termux API class

The PHP Termux API class provides wrappers to many services of the Termux:API app. It is very simple to use as you may see here.

Termux:API issues

Some Termux:API functions are long-lasting and resource-intensive like for instance the SMS inbox check, contact list, and location.

Since Termux programs are called is with shell_exec(), its synchronous behavior may delay seriously the Web Server approach. Consider to make an AJAX call to the methods to hide this issue.

Termux is a nice Android app that makes it easy to run PHP or other programs typically available in Linux distributions.

The possibility to run PHP either as a command line script or as a Web server opens a myriad of possibilities to run nice applications using PHP code like other native Android applications.

If you liked this article, share it with your developer friends. If you have questions or suggestions, post a comment here. 

This content was originally published here.