Control an Android Phone using PHP

Today i am telling you trick for Android in which you can control an Android phone using PHP by using Termux which is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required.

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).

Recommended hacking article:

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.

Features of Termux API

  • Secure. Access remote servers using the ssh client from OpenSSH. Termux combines standard packages with accurate terminal emulation in a beautiful open source solution.
  • Feature packed. Take your pick between Bash, fish or Zsh and nano, Emacs or Vim. Grep through your SMS inbox. Access API endpoints with curl and use rsync to store backups of your contact list on a remote server.
  • Customizable. Install what you want through the APT package management system known from Debian and Ubuntu GNU/Linux. Why not start with installing Git and syncing your dotfiles?
  • Explorable. Have you ever sat on a bus and wondered exactly which arguments tar accepts? Packages available in Termux are the same as those on Mac and Linux - install man pages on your phone and read them in one session while experimenting with them in another.
  • With batteries included. Can you imagine a more powerful yet elegant pocket calculator than a readline-powered Python console? Up-to-date versions of Perl, Python, Ruby and Node.js are all available.
  • Ready to scale up. Connect a Bluetooth keyboard and hook up your device to an external display if you need to - Termux supports keyboard shortcuts and has full mouse support.
  • Tinkerable. Develop by compiling C files with Clang and build your own projects with CMake and pkg-config. Both GDB and strace are available if you get stuck and need to debug.

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.

  1. Battery Status
  2. Camera Info
  3. Camera Photo
  4. Clipboard Get
  5. Clipboard Set
  6. Contact List
  7. Download file with Android Download Manager
  8. Location via GPS, Mobile Network or Wi-Fi
  9. Notification
  10. Open Url with default web browser
  11. Share file with Android actions
  12. SMS Inbox read
  13. SMS Send
  14. Telephony Device Info
  15. Toast show over the screen
  16. TTS Engines in Android
  17. TTS Speak
  18. Vibrate the Phone
  19. Wi-Fi Connection Info
  20. 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 
  • 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:
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

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.
php -S address:port -t /sdcard/php/rootdirectory/
If you use an address that is, the server listens to any of the device network IP addresses. if address is, 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.


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. 

No comments:

Post a Comment