PHP code example of bennito254 / routeros-client

1. Go to this page and download the library: Download bennito254/routeros-client 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/ */

    

bennito254 / routeros-client example snippets


use Bennito254\RouterOS\Client\Client;
use Bennito254\RouterOS\Query\Query;

',
    'username' => 'admin',
    'password' => 'password',
    'port' => 8728,
]);

// Send a command
$response = $client->query('/system/resource/print');

// Access results
foreach ($response->toArray() as $item) {
    echo "CPU Load: " . ($item['cpu-load'] ?? 'N/A') . "%\n";
}

use Bennito254\RouterOS\Laravel\Facade as RouterOS;

$response = RouterOS::query('/ip/address/print');

use Bennito254\RouterOS\Facade\IpAddress;
use Bennito254\RouterOS\Facade\HotspotUser;

// Using the IpAddress Facade
$ipFacade = new IpAddress($client);

// Get all IP Addresses
$addresses = $ipFacade->getAll();

// Add a new IP Address
$ipFacade->add([
    'address'   => '10.0.0.1/24',
    'interface' => 'ether2',
]);

// Disable a Hotspot user by their RouterOS `.id`
$hotspotFacade = new HotspotUser($client);
$hotspotFacade->disable('*1F');

$query = Query::make('/ip/address/print')
    ->where('interface', 'ether1')
    ->equal('disabled', false)
    ->tag('my-request');

$response = $client->query($query);

$client = new Client([
    'host' => '192.168.88.1',
    'username' => 'admin',
    'password' => 'password',
    'proxy' => [
        'type' => 'socks5',
        'host' => '127.0.0.1',
        'port' => 1080,
        // 'username' => 'proxyuser',
        // 'password' => 'proxypass',
    ],
]);

$client = new Client([
    'host' => '192.168.88.1',
    'username' => 'admin',
    'password' => 'password',
    'port' => 8729,
    'ssl' => [
        'enabled' => true,
        'verify_peer' => false, // Set to true in production
    ],
]);

use Bennito254\RouterOS\Exception\TrapException;

try {
    $client->query('/invalid/command');
} catch (TrapException $e) {
    echo "RouterOS Error: " . $e->getMessage();
}
bash
php artisan vendor:publish --tag="routeros-config"