PHP Classes

How to Use Termux Tutorial Running PHP Web Server or CLI with Termux PHP App to Control an Android Phone or Tablet - PHP Termux API package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Termux API PHP Termux API   Blog PHP Termux API package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use Termux Tut...  
  Post a comment Post a comment   See comments See comments (8)   Trackbacks (0)  

Author:

Viewers: 5,545

Last month viewers: 1,150

Package: PHP Termux API

The Termux application allows running many well known Linux applications on an Android phone, tablet or any other kind of device.

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.




Loaded Article

Contents

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

Termux:API issues

Conclusion


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 on Google Play Store

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.

  • 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

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

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

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://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

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.

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.

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 = 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:

3. Android Termux - Dane Hardin (2018-04-30 17:25)
Termux app for Android... - 0 replies
Read the whole comment and replies

2. Андроид - HanKrum (2017-02-16 10:09)
Относно бъ... - 2 replies
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



  Post a comment Post a comment   See comments See comments (8)   Trackbacks (0)  
  All package blogs All package blogs   PHP Termux API PHP Termux API   Blog PHP Termux API package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use Termux Tut...