anandaerditya/routeros-api-php

Mikrotik RouterOS API Communicator Module for PHP

1.6.2 2025-02-05 04:45 UTC

This package is auto-updated.

Last update: 2025-03-05 05:00:11 UTC


README

Static Badge Packagist Version GitHub Release

This library is using API Client Communicator under PHP Socket, thanks to the repository from EvilFreelance. This library provides built-in methods based on the available configuration under Winbox, with their respective commands and parameters. At the moment, this library supports RouterBoard and Cloud Router Switch with pre-6.43 and post-6.43 versions of RouterOS, as mentioned in this repository.

Changelog

1.6.2 ( Enhancement, Feature )

  1. Update commands and their parameters into the following methods:

    • router_switch(),
    • router_switch_port(),
    • router_switch_port_isolation(),
  2. Add new methods :

    • router_switch_rule()

1.5.3 ( Bugfix, Feature )

  1. Add new methods :
    • system_resource(),
    • system_resource_cpu(),
    • system_resource_irq(),
    • system_resource_pci(),
    • system_resource_usb(),
    • interface_bridge_calea(),
    • interface_bridge_filter(),

1.5.3 ( Bugfix )

  1. Add message handler for !trap, !re, !done, and !fatal type under the send() method.

1.4.2 ( Bugfix )

  1. Fix Mikrotik API response message handling under send() method;

1.3.22 ( Hotfix )

  1. Add new Exception : ErrorException;
  2. Add error handler for each method to catch any errors under mikrotik API.
  3. Add new methods :
    • ip_dhcp_server_lease()
    • ip_arp()
    • router_switch()
    • router_switch_port()

Requirements

  • PHP : Version 8.0 and up
  • ext-sockets : A Socket extension for PHP
  • RouterOS : pre-6.43 and post-6.43 (Tested on RouterBoard and Cloud Router Switch device)

How to Install

Under composer, just copy this command to begin add this library into your project.

composer require anandaerditya/routeros-api-php

Using the Library

If you are using Composer Project such Laravel Framework, you can just call the library under Erditya\RouterOSInstance class. As follows :

    include 'vendor/autoload.php';
    
    use Erditya\RouterOSInstance;
    
    $router = new RouterOSInstance();
    
    # Create connection
    $router = $router->connect(
        [
            'host' => '192.168.10.1',
            'user' => 'username',
            'pass' => 'password',
            'port' => 8728 
        ]
    );

    # Example : Fetch all IP Addresses
    if ($router->is_connected()) {
        # Get All Addresses
        var_dump($router->ip_addresses());
        
        # Get address with interface ether2
        var_dump($router->ip_addresses(['interface' => 'ether2']));
        
        # Update address item in ID *2
        var_dump($router->ip_addresses('set', [
            '.id' => '*2',
            'comment' => 'Some Comments'
        ]));
    }

Or, another way to approach :

    include 'vendor/autoload.php';
    
    use Erditya\RouterOSInstance;
    
    $router = new RouterOSInstance();
    
    # Create connection
    $router = $router->connect(
        [
            'host' => '192.168.10.1',
            'user' => 'username',
            'pass' => 'password',
            'port' => 8728 
        ]
    );

    # Get All Addresses
    var_dump($router->ip_addresses());
        
    # Get address with interface ether2
    var_dump($router->ip_addresses(['interface' => 'ether2']));
        
    # Update address item in ID *2
    var_dump($router->ip_addresses('set', [
        '.id' => '*2',
        'comment' => 'Some Comments'
    ]));

Available Methods

List of all methods available in this library at this version :

  1. General

    • connect()
    • is_connected()
  2. Interface

    • interface_bridge()
    • interface_bridge_port()
    • interface_bridge_port()
    • interface_ethernet()
    • interface_list()
    • interface_list_member()
    • interface_vlan()
  3. IP

    • ip_addresses()
    • ip_dhcp_server()
    • ip_dhcp_server_network()
    • ip_dns()
    • ip_firewall_filter()
    • ip_firewall_nat()
    • ip_hotspot_user_profiles()
    • ip_hotspot_users()
    • ip_hotspot_server_profiles()
    • ip_hotspot_servers()
    • ip_hotspot_ip_binding()
    • ip_pool()
    • ip_route()
    • ip_services()
  4. System

    • system_identity()
    • system_user_groups()
    • system_users()
  5. Switch (for Cloud Router Switch only)

    • switch_crs_egress_vlan_tag()
    • switch_crs_mac_based_vlan()
    • switch_crs_port()

Commands & Parameters, and Response

All the methods provided in this library are similar to Winbox's CLI terminal. You can check them under Winbox for more information. All the methods using the command print as the default returned the response as an array. If you call the method using another command, such as add, set, remove, etc., the method will return true, which indicates it was successfully executed.