Download the PHP package coimstark/mikrotik-api-php without Composer
On this page you can find all versions of the php package coimstark/mikrotik-api-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mikrotik-api-php
RouterOS API Client
This library is partly based on this old project, 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
- Minimum requirements
- Laravel framework support
- Laravel installation
- How to use
- How to configure the client
- List of available configuration parameters
- How to enable support of legacy login schema (RouterOS pre-6.43)
- How to write queries
- Read response as Iterator
- Short methods
- Known issues
- Unable to establish socket session, Operation timed out
- How to update/remove/create something via API?
- Undefined character (any non-English languages)
- Testing
- Links
Minimum requirements
php
>= 7.2|8.0ext-sockets
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:
Use the facade and pass array of parameters to client
method:
You also may get array with all configs which was obtained from routeros-api.php
file:
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 intoproviders
block of your config/app.php
:
Optionally, publish the configuration file if you want to change any defaults:
How to use
Basic example, analogue via command line is /ip hotspot ip-binding print
:
Basic example for update/create/delete types of queries:
If you need export all settings from router:
Examples with "where" conditions, "operations" and "tag":
All available examples you can find here.
How to configure the client
You just need create object of Client class with required parameters in array format:
ℹ️ Advanced examples of Config and Client classes usage
List of available configuration parameters
Parameter | Type | Default | Description |
---|---|---|---|
host | string | (required) Address of Mikrotik RouterOS | |
user | string | (required) Username | |
pass | string | (required) Password | |
port | int | RouterOS API port number for access (if not set use 8728 or 8729 if SSL enabled) | |
ssl | bool | false | Enable ssl support (if port is not set this parameter must change default port to ssl port) |
ssl_options | array | details | See https://www.php.net/manual/en/context.ssl.php |
legacy | bool | false | Deprecated, will be removed from 1.5.0: Support of legacy login scheme (true - pre 6.43, false - post 6.43) |
timeout | int | 10 | Max timeout for connecting to RouterOS (in seconds) |
socket_timeout | int | 30 | Max read timeout from RouterOS (in seconds) |
socket_blocking | bool | true | Set blocking mode on a socket stream |
socket_options | array | details | See https://www.php.net/manual/en/context.socket.php |
attempts | int | 10 | Count of attempts to establish TCP session |
delay | int | 1 | Delay between attempts in seconds |
ssh_port | int | 22 | Number of SSH port for exporting configuration |
ssh_timeout | int | 30 | Max timeout from router via SSH (in seconds) |
How to enable support of legacy login schema (RouterOS pre-6.43)
From 0.8.1 this is not important, version of firmware will be detected automatically. Deprecated, will be removed from 1.5.0
How to write queries
You can write absolutely any queries to your router, for this you need to create a "Query" object whose first argument is the required command, after this you can add the attributes of the command to "Query" object.
More about attributes and "words" from which these attributes should be created here.
More about "expressions", "where", "equal" and other filters/modifications of your query you can find here.
Simple usage examples of Query class:
ℹ️ Advanced examples of Query class usage
Read response as Iterator
By default, original solution of this client is not optimized for work with a large amount of results, only for small count of lines in response from RouterOS API.
But some routers may have (for example) 30000+ records in
their firewall list. Specifically for such tasks, a method
readAsIterator
has been added that converts the results
obtained from the router into a resource, with which it will
later be possible to work.
You could treat response as an array except using any array_* functions
Short methods
You can simplify your code and send then read from socket in one line:
Known issues
Unable to establish socket session, Operation timed out
This error means that the library cannot connect to your router, it may mean router turned off (then need turn on), or the API service not enabled.
Go to Mikrotik Router OS -> IP -> Services
and enable api
service.
Or via command line:
How to update/remove/create something via API?
Instead of ->where()
method of Query
class you need to
use ->equal()
method:
Undefined character (any non-English languages)
RouterOS does not support national languages, only English (and API of RouterOS too).
You can try to reproduce it via web, for example add the comment to any element of your system, then save and reload the page, you will see unreadable characters.
Testing
You can use my other project with RouterOS in Docker container for running unit testing on your computer, for this you just need to have Expect, Docker and Docker Compose.
Next clone the repo with RouterOS in Docker and exec
docker-compose up -d
, then you need preconfigure virtual routers
via preconf.tcl
script from root of routeros-api-php:
And after this you can run tests:
Links
- Cloud Hosted Router - Virtual images of RouterOS for your hypervisor
- RouterOS Manual:API - In case if you are wondering what is insane
All versions of mikrotik-api-php with dependencies
ext-sockets Version *