Author: Luis Martinez Ulloa
Posted on: 2017-02-15
Package: PHP Termux API
This allows running PHP either from the Termux shell as a console script or as a Web server.
Read this article to learn how to execute PHP scripts from inside a Android device and expose to PHP some Android functionalities using the Termux application.
Contents
What is PHP Termux API?
Accessing Android Device Features using the Termux API
Steps to have the full setup
Accessing the device using a keyboard
Transfer files via FTP
Running PHP from Termux in an endless loop
Running PHP as a Web Server from Termux
Using the PHP Termux API class
Termux:API issues
Conclusion
What is PHP Termux API?
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).
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.
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.
- Battery Status
- Camera Info
- Camera Photo
- Clipboard Get
- Clipboard Set
- Contact List
- Download file with Android Download Manager
- Location via GPS, Mobile Network or Wi-Fi
- Notification
- Open Url with default web browser
- Share file with Android actions
- SMS Inbox read
- SMS Send
- Telephony Device Info
- Toast show over the screen
- TTS Engines in Android
- TTS Speak
- Vibrate the Phone
- Wi-Fi Connection Info
- Wi-Fi Scan info
Steps to have the full setup
- Install and configure a more generic keyboard you know and use (like Hacker's Keyboard)
- Install and configure FTP Server you now and use (like FTP Server (demo))
- Install Termux and Termux:API add-on from some repo (F-Droid or Play Store)
- Inside Termux console, type:
apt update apt upgrade apt install termux-api php mkdir /sdcard/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).
Transfer files via FTP
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://user@ipaddress: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:
php endlessloop.php
Running PHP as a Web Server from Termux
In Termux, type In order to start the development Web server embedded in PHP.
php -S address:port -t /sdcard/php/rootdirectory/
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.
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 = new termuxapi(); echo $termux->location();
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.
Conclusion
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.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
2. Андроид - HanKrum (2017-02-16 10:09)
Относно бъ... - 1 reply
Read the whole comment and replies
1. access to serial usb - Francesco (2017-02-15 10:18)
communicate with arduino via termux + otg... - 3 replies
Read the whole comment and replies