PHP API cannot execute commands

Hello everyone,

I found this Mikrotik PHP API : https://github.com/BenMenking/routeros-api
and I need to execute very simple commands like change dns servers, en/dis eth port etc.

At first I show you what is my problem:

When I need to execute something like this (only reading output):

require('routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = false;
if ($API->connect('192.168.88.1', 'api', 'pass')) {

  $ARRAY = $API->comm("/ip/dns/print");
  print_r($ARRAY);
  print_r($ARRAY[0]['dynamic-servers']);
  $API->disconnect();
}

That’s working fine, but when I need to execute command like this:

$API->write('/ip/dns/set', false);
$API->write('=servers=', false);
$API->write('8.8.4.4');

That’s do nothing, but in router logs I can see this:

user api logged in from 192.168.88.25 via api
dns changed by api
user api logged out from 192.168.88.25

user api has full access and api is enabled.

I really don’t know what’s happen.
Thank for every answer

By the way, I need this three commands to execute:

First: Disable SSTP
interface sstp-client set 0 disabled=yes

Second: Monitor if SSTP is disabled
interface sstp-client monitor 0 once

Third: Enable SSTP
interface sstp-client set 0 disabled=no

Fourth: Monitor SSTP
interface sstp-client monitor 0 once


This is what I need, but I cannot understand router OS php api syntax…

Thanks

The property value is part of the word, and a new method call means a new word. So:

$API->write('=servers=8.8.4.4');



Then how about switching to a PHP API client that doesn’t require you to know the API protocol’s syntax, like the one from my signature maybe?

With it, you can do all you want with

<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$client = new RouterOS\Client('192.168.88.1', 'api', 'pass');

$client->sendSync(new RouterOS\Request('/ip dns set servers=8.8.4.4'));

$client->sendSync(new RouterOS\Request('/interface sstp-client set numbers=sstp-out1 disabled=yes'));
$monitorRequest = new RouterOS\Request('/interface sstp-client monitor numbers=sstp-out1 once');
$monitorResults = $client->sendSync($monitorRequest);
if ($monitorResults->getProperty('status') === 'disabled') {
    $client->sendSync(new RouterOS\Request('/interface sstp-client set numbers=sstp-out1 disabled=no'));
    $monitorResults = $client->sendSync($monitorRequest);
    if ($monitorResults->getProperty('status') !== 'disabled') {
        //Enabled, possibly disconnected; So what do we do now?
    }
}
 

Just replace “sstp-out1” with the actual name of your SSTP client interface.
(The API protocol in general doesn’t support numbers; It can only take IDs or names, which are confusingly specified via the “numbers” argument)

Very thanks :slight_smile:
It works

Thank you for quick response.