Community discussions

MikroTik App
 
hsimeonov
just joined
Topic Author
Posts: 4
Joined: Tue Jan 05, 2016 5:43 pm

PHP Api add user and password to Hotspot

Sun Feb 07, 2016 9:45 pm

Has anyone tried adding a user name and password to the Hotspot through the PHP APi?
Have read the API manual but can't seem to find examples or the right command.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP Api add user and password to Hotspot

Sun Feb 07, 2016 10:54 pm

Although you PM-ed me, and I've answered in the PM, for the benefit of anyone passing through this topic (from Google let's say):
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$util->setMenu('/ip hotspot user')->add(
    array(
        'name' => 'user1',
        'password' => 'password1',
        'profile' => 'profile1'
    )
);
 
And in general - whatever you'd do from the command line, it's analogous in the API.
 
chenashop
just joined
Posts: 6
Joined: Mon Apr 03, 2017 9:42 pm

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 12:08 am

trying with the code, I found following error

[root@localhost auto]# php test.php
PHP Parse error: syntax error, unexpected ''PEAR2_Net_RouterOS-1.0.0b5.ph' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/auto/test.php on line 5

my full code is

[root@localhost auto]# cat test.php

<?php

use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.230.41', 'admin', '123456'));

$util->setMenu('/ip hotspot user')->add(
    array(
        'name' => 'user1',
        'password' => 'password1',
        'profile' => 'profile1'
    )
);
?> 
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 1:42 am

@chenashop

Is your PHP version at least 5.3.0? Check with "php --version" if you're not sure, and if it is earlier, upgrade it.
 
chenashop
just joined
Posts: 6
Joined: Mon Apr 03, 2017 9:42 pm

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 6:31 am

I've two servers. both r having same error

[root@speed html]# php --version
PHP 5.3.3 (cli) (built: Feb 9 2016 10:21:34)

[root@localhost html]# php --version
PHP 5.6.28 (cli) (built: Nov 10 2016 16:14:06)


other api from same folder is working nicely .
 
chenashop
just joined
Posts: 6
Joined: Mon Apr 03, 2017 9:42 pm

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 8:39 am

I've replaced first 3 line from another working agi script . now getting error in another line :(

[root@localhost auto]# php test.php

PHP Parse error: syntax error, unexpected ''name'' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/auto/test.php on line 13

[root@localhost auto]# cat test.php
<?php

use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';
$util = new RouterOS\Util($client = new RouterOS\Client('192.168.230.41', 'admin', '123456'));

$util->setMenu('/ip hotspot user')->add(
    array(
        'name' => 'user1',
        'password' => 'password1',
        'profile' => 'profile1'
    )
);

?> 
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 2:00 pm

It seems the forum started using non breaking spaces even in code blocks for some reason, and that's what ended up breaking the whole thing.

I tried to replace the original code above with one that I just checked has normal spaces, but the one that ends up in the forum is still faulty :? .

The way to fix it "manually" is to simply remove all new lines and spaces, and create them again.

EDIT: Testing to see if having it in a new post will fix it... nope.
Testing to see if using "code" without "=php" will work... the first parsing error occurs later, inside the "add" method call.
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$util->setMenu('/ip hotspot user')->add(
    array(
        'name' => 'user1',
        'password' => 'password1',
        'profile' => 'profile1'
    )
);
 
chenashop
just joined
Posts: 6
Joined: Mon Apr 03, 2017 9:42 pm

Re: PHP Api add user and password to Hotspot

Wed Apr 05, 2017 2:16 pm

hehe
I typed the whole code myself and now its working . thanks lot !
 
boltbd1975
just joined
Posts: 1
Joined: Wed Mar 06, 2019 4:19 am

Re: PHP Api add user and password to Hotspot

Wed Mar 06, 2019 4:29 am

if there is an user already exists, how can i remove that user and add again with another password.

my problem is i want to use "mobile number" as username . so same user (same mobile number) may try several times for getting new password .

thanks in advance
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP Api add user and password to Hotspot

Mon Mar 25, 2019 12:18 am

Surround the add() with a try/catch, and in the catch, check if the error is the one you recognize and act accordingly. I.e. either remove and add the user (or you know, just set() them instead of remove(), then add()), or do what you do for all other errors (e.g. rethrow the exception).

Or you could use find() to check if the user exists before you make a decision to either add() or set(), and thus avoid getting that error in the first place... Though in that scenario, you're always making 2 API calls, instead of making 2 calls only when the user already exists. In other words, adding new users is slower.