how enter users with api for mikrotik for web

sorry my English is somewhat crude, I’m making a website for users to enter hotspot it through mikrotik API but I get an error and did not know what else to do, if anyone knows it would be greatly appreciated :frowning:

here the code

/////mk
$API = new routeros_api();

$API->debug = false;

if ($API->connect('xxx.xxx.xxx.xxx', '****', '*****')) {

$API->write('/ip/hotspot/user', array(
          "name" => "$nombre",
	                 	
));
}
   $API->disconnect();
  1. AFAIK, hotspot details need to be entered from the router page. What you can do with the API is to let users change their usernames, passwords, etc.

  2. What is $nombre? Is it defined in another part of the code? You don’t need to use quotes around it.

/ip/hotspot/user

This is a menu, not a command.

If what you’re trying to do is change the password for a user, you’ll need to first get their ID with an appropriate “print” command, then add this to the “numbers” argument of a new “set” command. The rest of the arguments in the “set” will reflect whatever you want changed.

I can show you how to do this with my package (the one from my signaure), but not with Denis’ class.

Good day … I just want to register users in the hotspot THROUGH .. a website without using the winbox, salutations

“Register users” as in “add a new user” or as in “login user”? Like I said, I don’t think login is possible with the API (though I don’t know for sure…).

Adding a new user is possible though. Just use the “add” command, i.e.

$API->write('/ip/hotspot/user/add', array(
          "name" => $nombre,
                       
)); 

(assuming $nombre carries the username to be added)

ok I understand, I ask because I saw there that if they could record and tie but with ip mac, I’m trying to modify it to hotspot, by the way the api

In the settings for each user, you can bind their name to an IP and/or MAC address.

In the same (separate!) web panel from which you allow web users to change their password, you could make a button which would take their current IP and change their old IP/MAC binding to their current IP and/or MAC.

I don’t get your question… could you paraphrase it please?

The tutorial (or whatever this linked thing is supposed to be) uses Denis’ class, because it has existed for much longer than my package. I released it only this September, whereas this thing is from March, when only Denis’ class was publically available.

sorry my english its so bad :confused: , you API is for what?

The same thing for which Denis’ class is for - to communicate with RouterOS using the MikroTik API.

The difference is mostly in syntax - Denis’ class forces you to read the API specification and make sure you act in accordance with it. What I do instead is to abstract the details away and instead focus on providing more intuitive ways to send requests and deal with responses. The developer is expected only to have basic understanding of RouterOS’ shell syntax (API syntax can still be used of course) and basic understanding of how to use objects in PHP.

Example:

$API = new routeros_api();

$API->debug = false;

if ($API->connect('xxx.xxx.xxx.xxx', 'nukeko', 'wkd4496')) {

$API->write('/ip/hotspot/user/add', array(
          "name" => $nombre,
                       
));
}
   $API->disconnect(); 

vs.

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', 'wkd4496');

//Define $nombre somewhere around or before here

$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);

$client->sendSync($addRequest); 

You probably don’t see much difference here. However, where my package shines is in readability of more complex requests.

For example to change the password, with my package you’d do

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', 'wkd4496');

//Define $nombre and $password somewhere around or before here

$nombreId = $client->sendSync(new Request('/ip hotspot user print .proplist=.id', null, Query::where('name', $nombre)))->getArgument('.id');

if (null === $nombreId) {
    echo "'{$nombre}' is not a registered username";
} else {
    $setRequest = new Request('/ip hotspot user set');

    $client->sendSync(
        $setRequest->setArgument('numbers', $nombreId)
        ->setArgument('password', $password)
    );
} 

And doing that is a little more complex with Denis’ class. It’s still possible… but IMHO, the above is both shorter and more readable way of doing it.

BTW, if you have PHP 5.4, you can (if you want to) even sqeeze the code above to:

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

//Define $nombre and $password somewhere around or before here

if (null === $nombreId = ($client = new Client('xxx.xxx.xxx.xxx', 'nukeko', 'wkd4496'))->sendSync(new Request('/ip hotspot user print .proplist=.id', null, Query::where('name', $nombre)))->getArgument('.id')) {
    echo "'{$nombre}' is not a registered username";
} else {
    $client((new Request('/ip hotspot user set'))->setArgument('numbers', $nombreId)
        ->setArgument('password', $password)
    );
} 

It’s not about “why”… it’s about “why not”. :laughing:

jajajjajaj :laughing: , ok thanks im try with that API

I solved the problem…thanks

im try to migrate for you API and i want to you see this code…

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here

$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$client->sendSync($addRequest);

tell me if that code is ok for add user in mikrotik…

thanks

Assuming $nombre and $profile are defined, and are a user name and profile, yes, that code should work.

mysql can be stored in the user id in mikrotik?

You mean if MySQL can be used to store the user ID from MikroTik? Yes. You’d have to store it as a VARCHAR (not a number), but still.

ok i make the database whit field (id_mk) but how include id mikrotik into (id_mk) mysql… i have only this code for include user…

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here

$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$client->sendSync($addRequest);

or is so complicated to do?

Simply print the user right after you add it.

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here

$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$addRequest->setArgument('id', $id_mk);
$client->sendSync($addRequest);

i can do something like this???
or

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here
$client->sendSync(new Request('/ip hotspot user print .proplist=.id', null, Query::where('.id', $id_mk)))->getArgument('id');
$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$addRequest->setArgument('id', $id_mk);
$client->sendSync($addRequest);

sorry for asking so much :frowning:

Errr… after you add it, not before.

So:

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here
$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$client->sendSync($addRequest);

$id = $client->sendSync(new Request('/ip hotspot user print .proplist=.id', null, Query::where('name', $nombre)))->getArgument('.id'); 



sorry for asking so much > :frowning: >

No problem. If you’re worried I’m starting to reduce the code samples I’m giving you - that’s because I’m trying to help you learn as opposed to spoon feeding you :wink: .

jajajajaja ok and thanks for you lessons…

then…

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
?>
  <?php
$nombre= htmlspecialchars($_POST['nombre']);
$password= htmlspecialchars($_POST['password']);
$profile= htmlspecialchars($_POST['profile']);

namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$client = new Client('xxx.xxx.xxx.xxx', 'nukeko', '******');

//Define $nombre somewhere around or before here
$addRequest = new Request('/ip hotspot user add');
$addRequest->setArgument('name', $nombre);
$addRequest->setArgument('profile', $profile);
$addRequest->setArgument('password', $password);
$client->sendSync($addRequest);


$sql2 = mysql_connect("localhost", "user_1", "");
mysql_select_db("usuario", $sql2);
mysql_query("insert into usuarios (nombre,profile,password) values ('$nombre','$profile', '$id_mk')",$sql2); 
$result = mysql_query($sql2); 
 mysql_close($sql2) 

?>

look
id => from mk
id_mk = from sql

i want to take id from mk and record into id_mk mysql is ok??

Your SQL query seems to be wrong… you’re writing the ID into the password field.

Beyond that, the rest are “stylistic” mistakes… it’s a good idea to use mysql_real_escape_string on your values, and I don’t know about you, but I like to keep my SQL queries readable by writing the SQL keywords in capitals, so that I know the names I’ve defined vs. the names of SQL keywords.

So, that’s

mysql_query(
"INSERT INTO usuarios (id_mk, nombre, profile, password) VALUES ('" .
    mysql_real_escape_string($id_mk, $sql2) . "', '" .
    mysql_real_escape_string($nombre, $sql2) . "', '" .
    mysql_real_escape_string($profile, $sql2) . "', '" .
    mysql_real_escape_string($password, $sql2) . "')",
$sql2
);
$result = mysql_query($sql2); 
mysql_close($sql2);