Page 1 of 1
PHP Api add user and password to Hotspot
Posted: Sun Feb 07, 2016 9:45 pm
by hsimeonov
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.
Re: PHP Api add user and password to Hotspot
Posted: Sun Feb 07, 2016 10:54 pm
by boen_robot
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.
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 12:08 am
by chenashop
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'
)
);
?>
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 1:42 am
by boen_robot
@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.
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 6:31 am
by chenashop
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 .
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 8:39 am
by chenashop
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'
)
);
?>
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 2:00 pm
by boen_robot
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
![Confused :?](./images/smilies/icon_confused.gif)
.
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'
)
);
Re: PHP Api add user and password to Hotspot
Posted: Wed Apr 05, 2017 2:16 pm
by chenashop
hehe
I typed the whole code myself and now its working . thanks lot !
Re: PHP Api add user and password to Hotspot
Posted: Wed Mar 06, 2019 4:29 am
by boltbd1975
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
Re: PHP Api add user and password to Hotspot
Posted: Mon Mar 25, 2019 12:18 am
by boen_robot
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.