PHPackages                             evilfreelancer/routeros-api-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. evilfreelancer/routeros-api-php

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

evilfreelancer/routeros-api-php
===============================

Modern Mikrotik RouterOS API PHP client for your applications (with Laravel support)

1.6.0(8mo ago)491206.1k—0.6%174[1 issues](https://github.com/EvilFreelancer/routeros-api-php/issues)[2 PRs](https://github.com/EvilFreelancer/routeros-api-php/pulls)3MITPHPPHP ^7.4|^8.0

Since Aug 19Pushed 8mo ago32 watchersCompare

[ Source](https://github.com/EvilFreelancer/routeros-api-php)[ Packagist](https://packagist.org/packages/evilfreelancer/routeros-api-php)[ Fund](https://boosty.to/evilfreelancer)[ Fund](https://pay.cloudtips.ru/p/937f48ac)[ RSS](/packages/evilfreelancer-routeros-api-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (37)Used By (3)

[![Latest Stable Version](https://camo.githubusercontent.com/3249c326313870ed4bde2f92fc44e9664bdae4ef3338058ad9f8a70e47ed8b8a/68747470733a2f2f706f7365722e707567782e6f72672f6576696c667265656c616e6365722f726f757465726f732d6170692d7068702f762f737461626c65)](https://packagist.org/packages/evilfreelancer/routeros-api-php)[![Total Downloads](https://camo.githubusercontent.com/9c700e5985c48d630027b0d68f01ee62ebef24dd4b13694f3c11d7ee2730df00/68747470733a2f2f706f7365722e707567782e6f72672f6576696c667265656c616e6365722f726f757465726f732d6170692d7068702f646f776e6c6f616473)](https://packagist.org/packages/evilfreelancer/routeros-api-php)[![License](https://camo.githubusercontent.com/f0b0346ab7c236e1f0b5d46aacf7ab92e2d137c65289e3f93fcdf0d9900c76ef/68747470733a2f2f706f7365722e707567782e6f72672f6576696c667265656c616e6365722f726f757465726f732d6170692d7068702f6c6963656e7365)](https://packagist.org/packages/evilfreelancer/routeros-api-php)

⚠️ Project Maintenance Notice

This project is currently unmaintained by the original author. Due to limited time and shifting priorities, I am unable to actively develop, debug, or support this project.

Key Points:

- Community contributions are welcome! Feel free to fork the repo and submit Pull Requests for fixes, improvements, or new features.
- I will review and merge reasonable PRs that align with the project’s scope.
- No guarantees are provided for issue resolutions, feature requests, or direct support.

Future updates depend entirely on community involvement.

Thank you for your understanding and contributions!

RouterOS API Client
===================

[](#routeros-api-client)

```
composer require evilfreelancer/routeros-api-php
```

This library is partly based on [this old project](https://github.com/BenMenking/routeros-api), but unlike it has many innovations to ease development. In addition, the project designed to work with PHP7/8 in accordance with the PSR standards.

You can use this library with pre-6.43 and post-6.43 versions of RouterOS firmware, it will be detected automatically on connection stage.

Table of Contents
-----------------

[](#table-of-contents)

- [Minimum requirements](#Minimum-requirements)
- [Laravel framework support](#Laravel-framework-support)
    - [Laravel installation](#Laravel-installation)
- [How to use](#How-to-use)
- [How to configure the client](#How-to-configure-the-client)
    - [List of available configuration parameters](#List-of-available-configuration-parameters)
    - [How to enable support of legacy login schema (RouterOS pre-6.43)](#How-to-enable-support-of-legacy-login-schema-(RouterOS-pre-6.43))
- [How to write queries](#How-to-write-queries)
- [Read response as Iterator](#Read-response-as-Iterator)
- [Short methods](#Short-methods)
- [Known issues](#Known-issues)
    - [Unable to establish socket session, Operation timed out](#Unable-to-establish-socket-session,-Operation-timed-out)
    - [How to update/remove/create something via API?](#How-to-update/remove/create-something-via-API?)
    - [Undefined character (any non-English languages)](#Undefined-character-(any-non-English-languages))
- [Testing](#Testing)
- [Links](#Links)

Minimum requirements
--------------------

[](#minimum-requirements)

- `php` &gt;= 7.2|8.0
- `ext-sockets`

Laravel framework support
-------------------------

[](#laravel-framework-support)

RouterOS API client is optimized for usage as normal Laravel package, all functional is available via `\RouterOS` facade, for access to client object you need instead:

```
$config = new \RouterOS\Config([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin',
    'port' => 8728,
]);
$client = new \RouterOS\Client($config);
```

Use the facade and pass array of parameters to `client` method:

```
$client = \RouterOS::client([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin',
    'port' => 8728,
]);
```

You also may get array with all configs which was obtained from `routeros-api.php` file:

```
$config = \RouterOS::config([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin',
    'port' => 8728,
]);

dump($config);

$client = \RouterOS::client($config);
```

### Laravel installation

[](#laravel-installation)

By default, the package will automatically register its service provider, but if you are a happy owner of Laravel version less than 5.5, then in a project, which is using the package (after `composer require` is done, of course), add into`providers` block of your `config/app.php`:

```
'providers' => [
    // ...
    RouterOS\Laravel\ServiceProvider::class,
],
```

#### Client configuration for Laravel

[](#client-configuration-for-laravel)

You could configure the Client using Environment variables. Complete list of available Environment variables you could find at the [default configuration file](https://github.com/EvilFreelancer/routeros-api-php/blob/master/configs/routeros-api.php).

Optionally you could publish the configuration file:

```
php artisan vendor:publish --provider="RouterOS\\Laravel\\ServiceProvider"
```

How to use
----------

[](#how-to-use)

Basic example, analogue via command line is `/ip hotspot ip-binding print`:

```
use \RouterOS\Client;
use \RouterOS\Query;

// Initiate client with config object
$client = new Client([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin',
    'port' => 8728,
]);

// Create "where" Query object for RouterOS
$query =
    (new Query('/ip/hotspot/ip-binding/print'))
        ->where('mac-address', '00:00:00:00:40:29');

// Send query and read response from RouterOS
$response = $client->query($query)->read();

var_dump($response);
```

Basic example for update/create/delete types of queries:

```
use \RouterOS\Client;
use \RouterOS\Query;

// Initiate client with config object
$client = new Client([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin'
]);

// Send "equal" query with details about IP address which should be created
$query =
    (new Query('/ip/hotspot/ip-binding/add'))
        ->equal('mac-address', '00:00:00:00:40:29')
        ->equal('type', 'bypassed')
        ->equal('comment', 'testcomment');

// Send query and read response from RouterOS (ordinary answer from update/create/delete queries has empty body)
$response = $client->query($query)->read();

var_dump($response);
```

If you need export all settings from router:

```
use \RouterOS\Client;

// Initiate client with config object
$client = new Client([
    'host'        => '192.168.1.3',
    'user'        => 'admin',
    'pass'        => 'admin',
    'ssh_port'    => 22222,
    'ssh_timeout' => 60, // if not set then 30 seconds by default
]);

// Execute export command via ssh
$response = $client->query('/export');
// or
$response = $client->export();

var_dump($response);
```

Examples with "where" conditions, "operations" and "tag":

```
use \RouterOS\Query;

/**
 * Simple "where" query will be generated by default
 */

$client->query('/ip/address/print')->read();

/**
 * Send advanced "where" query with parameters to RouterOS
 */

// If only one "where" condition
$client->query('/queue/simple/print', ['target', '192.168.1.1/32']);

// If multiple "where" conditions and need merge (operation "|") results
$client->query('/interface/print', [
    ['type', 'ether'],  // same as ['type', '=', 'ether']
    ['type', 'vlan'],   // same as ['type', '=', 'vlan']
], '|');

/**
 * Or in OOP style
 */

// If you need create query for "create/update/delete" operations
$query = new Query('/ip/hotspot/ip-binding/add');
$query->equal('mac-address', '00:00:00:00:40:29');
$query->equal('type', 'bypassed');
$query->equal('comment', 'testcomment');

// If multiple "where" conditions and need merge (operation "|") results
$query = new Query('/interface/print');
$query->where('type', 'ether');
$query->where('type', 'vlan');
$query->operations('|');

// If multiple "where" conditions and need append tag
$query = new Query('/interface/set');
$query->where('disabled', 'no');
$query->where('.id', 'ether1');
$query->tag(4);

/**
 * Write Query object to RouterOS and read response from it
 */

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

> All available examples you can find [here](https://github.com/EvilFreelancer/routeros-api-php/tree/master/examples).

How to configure the client
---------------------------

[](#how-to-configure-the-client)

You just need create object of Client class with required parameters in array format:

```
use \RouterOS\Client;

$client = new Client([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin'
]);
```

*ℹ️ Advanced examples of Config and Client classes usage*```
use \RouterOS\Config;
use \RouterOS\Client;

/**
 * You can create object of Config class
 */

$config = new Config();

// Then set parameters of config
$config->set('host', '192.168.1.3');
$config->set('user', 'admin');
$config->set('pass', 'admin');

// By the way, `->set()` method is support inline style of syntax
$config
    ->set('host', '192.168.1.3')
    ->set('user', 'admin')
    ->set('pass', 'admin');

/**
 * Or just create preconfigured Config object
 */

$config = new Config([
    'host' => '192.168.1.3',
    'user' => 'admin',
    'pass' => 'admin'
]);

/**
 * Then send Config object to Client constructor
 */

$client = new Client($config);
```

### List of available configuration parameters

[](#list-of-available-configuration-parameters)

ParameterTypeDefaultDescriptionhoststring(required) Address of Mikrotik RouterOSuserstring(required) Usernamepassstring(required) PasswordportintRouterOS API port number for access (if not set use 8728 or 8729 if SSL enabled)sslboolfalseEnable ssl support (if port is not set this parameter must change default port to ssl port)ssl\_optionsarray[details](https://github.com/EvilFreelancer/routeros-api-php/blob/master/src/Config.php#L46)See legacyboolfalseDeprecated, will be removed from 1.5.0: Support of legacy login scheme (true - pre 6.43, false - post 6.43)timeoutint10Max timeout for connecting to RouterOS (in seconds)socket\_timeoutint30Max read timeout from RouterOS (in seconds)socket\_blockingbooltrueSet blocking mode on a socket streamsocket\_optionsarray[details](https://github.com/EvilFreelancer/routeros-api-php/blob/master/src/Config.php#L87)See attemptsint10Count of attempts to establish TCP sessiondelayint1Delay between attempts in secondsssh\_portint22Number of SSH port for exporting configurationssh\_timeoutint30Max timeout from router via SSH (in seconds)ssh\_private\_keystring~/.ssh/id\_rsaFull path to required private key### How to enable support of legacy login schema (RouterOS pre-6.43)

[](#how-to-enable-support-of-legacy-login-schema-routeros-pre-643)

> From 0.8.1 this is not important, version of firmware will be detected automatically. Deprecated, will be removed from 1.5.0

```
