PHPackages                             hannes-kruger/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. [HTTP &amp; Networking](/categories/http)
4. /
5. hannes-kruger/routeros-api-php

ActiveLibrary[HTTP &amp; Networking](/categories/http)

hannes-kruger/routeros-api-php
==============================

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

001PHP

Since Oct 8Pushed 3y agoCompare

[ Source](https://github.com/Hannes-Kruger/routeros-api-php)[ Packagist](https://packagist.org/packages/hannes-kruger/routeros-api-php)[ RSS](/packages/hannes-kruger-routeros-api-php/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/3249c326313870ed4bde2f92fc44e9664bdae4ef3338058ad9f8a70e47ed8b8a/68747470733a2f2f706f7365722e707567782e6f72672f6576696c667265656c616e6365722f726f757465726f732d6170692d7068702f762f737461626c65)](https://packagist.org/packages/evilfreelancer/routeros-api-php)[![Build Status](https://camo.githubusercontent.com/ba2dcb198f4d7dc156fa3225d73455cba825a47d284e0803d2555cb48b82c1ac/68747470733a2f2f7472617669732d63692e6f72672f4576696c467265656c616e6365722f726f757465726f732d6170692d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/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)[![Code Climate](https://camo.githubusercontent.com/1e74c0651cd8a2cf727c07352b11bc285851babec7592b5efc0dbc9071bd06ab/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f4576696c467265656c616e6365722f726f757465726f732d6170692d7068702f6261646765732f6770612e737667)](https://codeclimate.com/github/EvilFreelancer/routeros-api-php)[![Code Coverage](https://camo.githubusercontent.com/4acf7916be758d5e60bd8dfa03beed158ccc63875554b293a76f2c3936b0f048/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4576696c467265656c616e6365722f726f757465726f732d6170692d7068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/EvilFreelancer/routeros-api-php/?branch=master)[![Scrutinizer CQ](https://camo.githubusercontent.com/d68a2a3d0408342581a26b891f20209e3d42157d1ca4eef4d02f9b70218b195a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6576696c667265656c616e6365722f726f757465726f732d6170692d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/evilfreelancer/routeros-api-php/)

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,
],
```

Optionally, publish the configuration file if you want to change any defaults:

```
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)### 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

```
