1. Go to this page and download the library: Download xvq/php-adb library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xvq / php-adb example snippets
use Xvq\PhpAdb\AdbClient;
$adb = new AdbClient('127.0.0.1',5037);
$devices = $adb->getAllDevices();
$device = $devices[0];
$serials = $adb->getAllDeviceSerials();
//serial
$device = $adb->device($serials[0]);
$device = $adb->device('8e257561');
//transportId
$device = $adb->device(transportId: 2);
//when there is only one device, no arguments need to be provided.
$device = $adb->device();
$output = $adb->connect("192.168.1.10:5555");
// output: connected to 192.168.1.10:5555
$output = $adb->disconnect("192.168.1.10:5555");
// output: disconnected from 192.168.1.10:5555
// Push and install an APK from the local filesystem to the device.
// The file './test.apk' will be uploaded to the device and then installed.
$result = $device->app->pushAndInstall('./test.apk');
// Install an APK that already exists on the device's storage.
$result = $device->app->install('/sdcard/test2.apk');
// $result 1 = success, 2 = need manual confirmation
$device->app->start('com.example.test');
// Get the current activity that is in focus.
$result = $device->app->currentFocus();
echo $result['package'];
echo $result['activity'];
echo $result['pid'];
$device->app->stop('com.example.test');
$device->app->clear('com.example.test');
$device->app->uninstall('com.example.test');
foreach ($device->app->list() as $packageName){
echo $packageName;
}
$info = $device->app->info('com.example.test');
// $info is an associative array containing details such as:
[
'package_name' => 'com.example.test',
'version_name' => '1.0.0',
'version_code' => 100,
'flags' => 'SYSTEM',
'first_install_time' => '2024-05-01 10:30:00',
'last_update_time' => '2024-05-10 15:42:00',
'signature' => 'AB:CD:EF:...',
'path' => '/data/app/com.example.test/base.apk',
'sub_apk_paths' => [],
]
use Xvq\PhpAdb\Enum\KeyCode;
// Simulate pressing the Home button
$device->input->keyEvent(KeyCode::KEY_HOME);
// Simulate typing text
$device->input->sendText('Hello World');
// Simulate tapping at (100px, 100px)
$device->input->tap(100, 100);
// Simulate tapping the screen center (50%, 50%) — decimals represent screen percentage
$device->input->tap(0.5, 0.5);
// Simulate swipe from (100px,100px) → (200px,200px) in 0.5 seconds
$device->input->swipe(100, 100, 200, 200, 0.5);
// Simulate swipe from (10%,50%) → (90%,50%) in 0.5 seconds — decimals represent screen percentage
$device->input->swipe(0.1, 0.5, 0.9, 0.5, 0.5);
// Simulate drag from (100px,100px) → (200px,200px) in 0.5 seconds
$device->input->drag(100, 100, 200, 200, 0.5);
// Simulate mouse wheel scroll down by one unit
$device->input->roll(0, 1);
// Simulate mouse wheel scroll up by one unit
$device->input->roll(0, -1);
$device->reboot();
// get device state ,[device,offline,bootloader]
$device->getState();
$v = $device->androidVersion();
// get device info
$info = $device->info;
echo $info->serial;
echo $info->transportId;
echo $info->product;
echo $info->model;
echo $info->device;
//adb root
$device->root();
// get device features
$device->getFeatures();
// open browser
$device->shell->openBrowser("https://www.baidu.com");
// is screen on
$device->shell->isScreenOn();
// get window size
$arr = $device->shell->getWindowSize();
echo $arr['width'];
echo $arr['height'];
// There are many other available methods as well
// please refer to the source code for details.
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.