-
Notifications
You must be signed in to change notification settings - Fork 273
Installation
- PHP 5.4 or newer (the codebase is plain procedural-style OOP, no PHP 8 only syntax is used, so it runs fine on modern PHP too)
- The
sockets-adjacent stream functions, which are part of PHP core (stream_socket_client,fread,fwrite), no extra extension needed for plain connections - The
opensslPHP extension, only if you plan to connect overapi-ssl(see SSL and TLS Connections) - Network access from the machine running the script to the router on the API port, usually 8728 for plain API or 8729 for API-SSL
composer require ben-menking/routeros-apiThe package uses Composer's classmap autoloading, not PSR-4, so there is no namespace to import. After installing, the global class RouterosAPI is simply available once Composer's autoloader is loaded:
<?php
require 'vendor/autoload.php';
$API = new RouterosAPI();Since this is a single file with no dependencies, you can skip Composer entirely:
<?php
require __DIR__ . '/routeros_api.class.php';
$API = new RouterosAPI();This is the same approach used in the examples/ folder in this repository.
The PHP class is only half the story. RouterOS does not expose the API service by default on a fresh install, and this is the single most common reason a connection silently fails. On the router itself:
/ip service print
Confirm api (port 8728) or api-ssl (port 8729) is enabled and not restricted by an address list that excludes the host you are connecting from. To enable the plain API service:
/ip service set api disabled=no
If you only want API-SSL, leave api disabled and enable api-ssl instead, then read SSL and TLS Connections before writing any PHP.
Also check /ip firewall filter on the router. A default "drop everything not explicitly allowed" input chain will block API connections from anywhere outside the LAN unless you add an explicit accept rule for the API port and source range you are connecting from.
Continue to Quick Start for a full working connection example.
Documentation contributed by Zill E Ali, Network Engineer and creator of mikrotik-laravel | GitHub | LinkedIn
Documentation maintained by Zill E Ali Network Engineer, MikroTik RouterOS / ISP infrastructure specialist, MTCNA Certified
- GitHub: github.com/Zilleali
- Website: zilleali.com
- LinkedIn: linkedin.com/in/zilleali12
Found an error or have something to add? Open a PR against this wiki or reach out directly.