PHP-API : Add new user for hotspot and Change user password

Hi,

  1. Is there any example for me to add user hotspot using php-api ?

  2. I’m using http://wiki.mikrotik.com/wiki/API_PHP_package to change user password so user can change their password but not works. Here are the code :

<?php
namespace PEAR2\Net\RouterOS;
require_once 'include/classes/PEAR2_Net_RouterOS-1.0.0b3.phar';

$errors = array();

try {
    //Adjust RouterOS IP, username and password accordingly.
    $client = new Client('192.168.0.1', 'xxxxxx', 'xxxxxxxxx');

    //$printRequest = new Request('/ip hotspot active print');
    //$printRequest->setQuery(Query::where('address', $_SERVER['REMOTE_ADDR']));
    //$activeUserEntry = $client->sendSync($printRequest);

    $printRequest = new Request('/ip hotspot active print .proplist=user,address');
    $printRequest->setQuery(Query::where('address', $_SERVER['REMOTE_ADDR']));
    $activeUserEntry = $client->sendSync($printRequest);

} catch(\Exception $e) {
    $errors[] = $e->getMessage();
}

if (isset($_POST['password']) && isset($_POST['password2'])) {
    if ($_POST['password'] !== $_POST['password2']) {
        $errors[] = 'Passwords do not match.';
    } else {
        //Here's the fun part - actually changing the password
        $setRequest = new Request('/ip hotspot users set');
        $client($setRequest
            ->setArgument('password', $_POST['password'])
            ->setArgument('numbers', $client($printRequest
                ->setCommand('/ip hotspot user print')
                ->setArgument('.proplist', '.id')
                ->setQuery(Query::where('name', $activeUserEntry->getArgument('user')))
                )->getArgument('.id'))
        );
echo " DOne";
    }
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Change your hotspot password</title>
        <style type="text/css">#errors {background-color:darkred;color:white;}</style>
    </head>
    <body>
        <div>
            <?php if (!isset($activeUserEntry)) { ?>
                <h1>We're sorry, but we can't change your password right now.
                Please try again later</h1>
            <?php } else { 
            	?>
            <h1>You are currently logged in as "<?php
                    echo $activeUserEntry->getArgument('user');
                ?>"</h1>

            <?php if(!empty($errors)) { ?>
            <div id="errors"><ul>
                <?php foreach ($errors as $error) { ?>
                <li><?php echo $error; ?></li>
                <?php } ?>
            </ul></div>
            <?php } ?>

            <form action="" method="post">
                <ul>
                    <li>
                        <label for="password">New password:</label>
                        <input type="password" id="password" name="password" value="" />
                    </li>
                    <li>
                        <label for="password2">Confirm new password:</label>
                        <input type="password" id="password2" name="password2" value="" />
                    </li>
                    <li>
                        <input type="submit" id="act" name="act" value="Change password" />
                    </li>
                </ul>
            </form>
            <?php } ?>
        </div>
    </body>
</html>

The user password still not change. What is the problem with above code ?

I actually hadn’t tested this code, because I don’t have hotspot at my setup.

Thank you for taking the time to try it out. On a closer inspection, the problem may be the line

$setRequest = new Request('/ip hotspot users set'); 

which should instead be

$setRequest = new Request('/ip hotspot user set'); 

(notice the last “s” in “users”)

I’ve now corrected it at the wiki, along with the other error I see you’ve already noticed - that the active user’s username is at “user” and not “name”.

Ah.. that’s the problem… now the script is working like a charm.

boen_robot, do you have an example for add a new user for hotspot ?

Ah.. yes, it’s working now after change ‘users’ to ‘user’.

boen_robot, do you have an example for add a new user hostpot using your API ?

It’s the same as adding to any list. The examples at the GitHub wiki use the ARP list as an example, but it’s the same deal with hotspot users, profiles or whatever.

The rest is simply having some HTML forms to take the data from (i.e. “basic PHP”).

The key bit:

$addRequest = new Request('/ip hotspot user add profile=default');
$addRequest
    ->setArgument('name', $_POST['username'])
    ->setArgument('password', $_POST['password']);

$errors = $client->sendSync($addRequest)->getAllOfType(Response::TYPE_ERROR);
if (!count($errors)) {
    //Unable to add user. Handle errors here.
} 

hello boen_robot,

i just read your post and it works fine. thank you.

here the code called addUser.php, so i can call it like ‘php addUser.php test1 pass1’

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

$username = $argv[1];
$password = $argv[2];

$client = new Client('10.10.10.30', 'api', 'api');
$addRequest = new Request('/ip hotspot user add profile=default');
$addRequest
    ->setArgument('name', $username)
    ->setArgument('password', $password);

$errors = $client->sendSync($addRequest)->getAllOfType(Response::TYPE_ERROR);
if (!count($errors)) {
    //Unable to add user. Handle errors here.
}
?>

question is, any example to remove the user ?

Thank you