Mikrotik PHP API probelm

I am developing a PHP application using Mikrotik API for customer self registration. I am facing a weird problem. I can create a user using PHP scriot. The problem is, some of the users doesn’t have any profile with username. Also, deleting those users by API also not possible. I am expecting expert advice in this matter.

Does the same problem also occur from terminal? Which RouterOS version are you using? What’s an example of a command call that fails?

Than you for your help. The commands working without any problem through terminal. The problem occurring randomly when done through PHP script. I am using latest version of Router OS.

Some sample code (that fails (randomly?!?; that’s another thing…)) please?

$API = new RouterosAPI();

		if ($API->connect()) 
		{
	    //Delete existing user. This will fail if the user doesn't have a profile	
            $DelUser = $API->comm("/tool/user-manager/user/remove", array(
								          		"numbers"  => $roomnop,));
            
			$newUser = $API->comm("/tool/user-manager/user/add", array(
						          "customer" => "admin",
						          "username" => $roomnop,
						          "password" => $passwordp,));
			$add_user_profile = $API->comm("/tool/user-manager/user/create-and-activate-profile", array(
												"customer" => "admin",
								          		"numbers"  => $roomnop,
								          		"profile"  => $profilep,));
                $API->disconnect();  
		}

Hi, this is the sample code. The code randomly failed to add profile to a user. For example, the code is creating a user with username ‘X’ and profile ‘Y’. Some time, the user doesn’t have a profile. Another problem is, those user created by the PHP code without a profile, can’t be deleted using API+PHP code. We want to delete those users using console.

$roomnop sounds like “room number”… as in, an integer… is it? Although User Manager allows that, I think that part might be error prone. You may want to prefix the username with something, so that it doesn’t start with a number… say “room-”.

Also, I’m pretty sure create-and-activate-profile requires a profile. If a user “doesn’t have a profile” (as in “they aren’t limited in any way”), create a profile that doesn’t have any limitations, and use that. Alternatively, don’t call create-and-activate-profile at all.


For the “random” part… I suggest you try the client from my signature. I’m not aware of it having any randomly occurring problems - it should always fail to create-and-activate-profile when the profile is not there, or if that’s allowed - always succeed… Unless it’s a RouterOS issue, which in this case I doubt.

Rewriting your code with it in mind:

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

//Define $roomnop, $password and $profilep here

try {
    $client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

    $removeRequest = new RouterOS\Request('/tool/user-manager/user/remove');
    $removeRequest->setArgument('numbers', $roomnop)
    $DelUser = $client->sendSync($removeRequest);
    
    $addRequest = new RouterOS\Request('/tool/user-manager/user/add');
    $addRequest
        ->setArgument('customer', 'admin')
        ->setArgument('username', $roomnop)
        ->setArgument('password', $password);
    $newUser = $client->sendSync($addRequest);

    $activateRequest = new RouterOS\Request('/tool/user-manager/user/create-and-activate-profile');
    $activateRequest
        ->setArgument('customer', 'admin')
        ->setArgument('username', $roomnop)
        ->setArgument('profile', $profilep);
    $add_user_profile = $client->sendSync($activateRequest);
} catch (Exception $e) {
} 

All usernames are integers. It’s a self-care system. The guest want to create a user with the help of reception (For unique ID). Also the profile fields are mandatory. So the problem is either related to the PHP script or the API or even Router OS itself.

What might be the reason for inability to delete a user (Which doesn’t have a profile) using the PHP code ? If you can suggest a way to delete such users, then my problem is solved.

Your library look cool and easy to use. Unfortunately, I can’t rewrite the whole code as I have some difficulties for that, and the time to finish the project is already over.
Thanks for your reply.

If you have some JavaScript in your form that does something with the profile part, it could be a browser issue (incompatibility with some JavaScript thing), or perhaps even a timing issue (device is too slow/fast for what JavaScript expects at a point).

A quick fix would be to simply check whether the profile is set when receiving, and specify a default profile when it isn’t, e.g.

if (empty($_POST['profile']) || !in_array($_POST['profile'], array('default', 'slow', 'fast'))) {
    $profilep = 'default';
} else {
    $profilep = $_POST['profile'];
}

(assuming valid profile names are “default”, “slow” and “fast”)

First, thanks for your code. The software have client side validation for selecting a profile. Without selecting a profile, the user can’t register.

Another problem, why I can’t delete a user using API, who have no profile.

Any help from experts ?

Client side validation can be bypassed or “missed”, so you should have the above code regardless.

Could be a RouterOS bug. One way to workaround it is to call a “print” with a query that tests for the username. Extract the “.id” out of the match, and use that as the value for “numbers”.

Can’t get ‘id’ for a user.

 print detail where username =510H
 
 0     customer=admin username="510H" password="321" shared-users=1 wireless-psk="" wireless-enc-key="" wireless-enc-algo=none uptime-used=1d33s download-used=1649974649 upload-used=133415505 last-seen=feb/10/2016 14:30:47

Please let me know how to get ‘id’ for a user.

It’s “.id” (notice the dot), and it’s visible only from API. When I said you should call “print”, I meant call it from the API.

Ok, thanks for your reply. I will try and post the result.