PHP code example of joserf / mikrotik-php-api

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

    

joserf / mikrotik-php-api example snippets


$config =
    (new Config())
        ->set('host', '192.168.*.*')
        ->set('port', 8728) 
        ->set('user', 'LOGIN')
        ->set('pass', 'SENHA');


r_reporting(E_ALL);

use \RouterOS\Config;
use \RouterOS\Client;
use \RouterOS\Query;

// Create config object with parameters
$config =
    (new Config())
        ->set('host', '192.168.*.*')
        ->set('user', 'LOGIN')
        ->set('pass', 'SENHA');

// Initiate client with config object
$client = new Client($config);

// Build query (Get resources in RouterOS)
$query = new Query("/system/resource/print");

// Send query to RouterOS
$request = $client->query($query);

// Read answer from RouterOS
$response = $client->read();

// Print result in json format
print_r(json_encode($response, JSON_PRETTY_PRINT));


// Build monitoring query (/interface monitor-traffic interface=ether1)
$query =
    (new Query('/interface/monitor-traffic'))
        ->equal('interface', 'ether1')
        ->equal('once');
        
// Monitoring details
$out = $client->query($query)->read();
print_r($out);        
        

// show only rx-bits-per-second
print_r($out [0]["rx-bits-per-second"]);

// Build query (Get users active in RouterOS)
$query = new Query("/ip/hotspot/active/print");

// Count total user active in RouterOS
print_r(count($response));

// Build query (Get users active in RouterOS)
$query = new Query("/ip/hotspot/active/print");

// Print result in JSON format
print_r(json_encode($response, JSON_PRETTY_PRINT));


// Build query (Get resources in RouterOS)
$query = new Query("/system/resource/print");

// Show uptime active in RouterOS
echo 'Uptime: ' . json_encode($response[0]['uptime']);

shell
composer