Send command to mikrotik from API with PHP

0

Hi, I would like you to help me create a php that sends an order to my mikrotik, the exact command that I want to execute within the mikrotik would be this:

  

/ user add name = api group = full password = api comment = userapi.

I have already tried several ways but the command that I have in php when I run it shows in the mikrotik: user admin logged in from via api but it does not perform the command, The php I use is this if someone could help me I would appreciate it

<?php

require('routeros_api.class.php');

$API = new RouterosAPI();

$API->debug = true;

if ($API->connect('192.168.7.3', 'admin', '123')) {

   $API->write('/user add name=api group=full password=api comment=usuario api');

   $READ = $API->read(false);
   $ARRAY = $API->parse_response($READ);

   print_r($ARRAY);

   $API->disconnect();

}

?>
    
asked by Carlos Manuel Grass Torres 28.10.2018 в 01:45
source

1 answer

0

The problem is in the way you are passing the parameters. First you have to write the command, then each of the parameters. For this, the write function accepts a second argument that allows you to indicate that you are going to send more information.

$API->write('/user add', false);
$API->write('name=api', false);
$API->write('group=full', false);
$API->write('password=api', false);
$API->write('comment={usuario api}', true);

Although, if you want to simplify your life, the api also brings a function called comm that allows you to do it like this:

$API->comm('/user add', array(
    "name"     => "api",
    "group"    => "full",
    "password" => "api",
    "comment"  => "{usuario api}"
));

Disclaimer

In my life I played a mikrotik. I took the information from the examples and comments of the code in the API repository .

    
answered by 28.10.2018 в 02:23