PHP code example of mivodev / mikrotik-api-ros6

1. Go to this page and download the library: Download mivodev/mikrotik-api-ros6 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/ */

    

mivodev / mikrotik-api-ros6 example snippets




use Mivo\MikrotikRos6\Client;

$api = new Client();
$api->connect('192.168.1.1', 'admin', 'password');

// Get router identity
$identity = $api->comm('/system/identity/print');
print_r($identity);

$api->disconnect();

use Mivo\MikrotikRos6\Client;

$api = new Client();

// Basic connection
$api->connect('192.168.1.1', 'admin', 'password');

// Custom port
$api->connect('192.168.1.1', 'admin', 'password', 8728);

// SSL connection (port 8729)
$api->ssl = true;
$api->connect('192.168.1.1', 'admin', 'password', 8729);

$api = new Client();

$api->timeout  = 3;     // Connection timeout in seconds (default: 3)
$api->attempts = 5;     // Number of retry attempts (default: 5)
$api->delay    = 3;     // Delay between retries in seconds (default: 3)
$api->ssl      = false; // Use SSL connection (default: false)
$api->debug    = false; // Enable debug output (default: false)

// ─── READ (print) ───────────────────────────────────
$users    = $api->comm('/ip/hotspot/user/print');
$queues   = $api->comm('/queue/simple/print');
$addrs    = $api->comm('/ip/address/print');
$secrets  = $api->comm('/ppp/secret/print');
$ifaces   = $api->comm('/interface/print');
$firewall = $api->comm('/ip/firewall/filter/print');
$dns      = $api->comm('/ip/dns/print');
$routes   = $api->comm('/ip/route/print');
$resource = $api->comm('/system/resource/print');

// ─── CREATE (add) ───────────────────────────────────
$api->comm('/ip/hotspot/user/add', [
    'name'     => 'pelanggan-baru',
    'password' => 'rahasia123',
    'profile'  => 'paket-50mbps',
]);

$api->comm('/ppp/secret/add', [
    'name'     => 'pppoe-user1',
    'password' => 'secret',
    'service'  => 'pppoe',
    'profile'  => 'default',
]);

$api->comm('/queue/simple/add', [
    'name'       => 'queue-pelanggan',
    'target'     => '192.168.1.100/32',
    'max-limit'  => '10M/10M',
]);

// ─── UPDATE (set) ───────────────────────────────────
$api->comm('/ip/hotspot/user/set', [
    '.id'      => '*1',
    'password' => 'password-baru',
]);

// ─── DELETE (remove) ────────────────────────────────
$api->comm('/ip/hotspot/user/remove', [
    '.id' => '*1',
]);

// ─── SYSTEM OPERATIONS ─────────────────────────────
$api->comm('/system/reboot');
$api->comm('/system/identity/set', [
    'name' => 'Router-Mivo',
]);

// Filter by exact value
$activeUsers = $api->comm('/ip/hotspot/active/print', [
    '?user' => 'pelanggan-baru',
]);

// Filter PPPoE secrets by service type
$pppoeOnly = $api->comm('/ppp/secret/print', [
    '?service' => 'pppoe',
]);

// Filter by profile
$premium = $api->comm('/ip/hotspot/user/print', [
    '?profile' => 'paket-premium',
]);

// Find users whose name matches a pattern
$matched = $api->comm('/ip/hotspot/user/print', [
    '~name' => 'pelanggan-.*',
]);

$users = $api->comm('/ip/hotspot/user/print');

// Returns:
// [
//     ['.id' => '*1', 'name' => 'admin', 'profile' => 'default'],
//     ['.id' => '*2', 'name' => 'pelanggan1', 'profile' => 'paket-50mbps'],
// ]

foreach ($users as $user) {
    echo $user['name'] . ' — ' . $user['profile'] . "\n";
}

use Mivo\MikrotikRos6\Client;
use Mivo\MikrotikRos6\Exceptions\MikrotikException;

$api = new Client();

try {
    $api->connect('192.168.1.1', 'admin', 'wrong-password');
} catch (MikrotikException $e) {
    echo "Connection failed: " . $e->getMessage();
}

try {
    $result = $api->comm('/nonexistent/command');
} catch (MikrotikException $e) {
    echo "Command failed: " . $e->getMessage();
}

$api = new Client();
$api->debug = true;
$api->connect('192.168.1.1', 'admin', 'password');

// Output:
// Connection attempt #1 to 192.168.1.1:8728...
// <<< [/login]
// <<< [=name=admin]
// <<< [=password=password]
// >>> [!done]
// Connected and authenticated.