Community discussions

MikroTik App
 
brasileottanta
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Thu Jun 22, 2006 8:52 am

RouterOs php api .

Fri May 24, 2013 10:15 pm

Hi ,

I need a php script that delete a host from hotspot/ip binding.

I try this :
<?php

require('router_class.api.php');
$API = new routeros_api();
$API->debug = true;
if ($API->connect('1.1.1.1,'xxxxx','xxxxxx')) {
   $API->comm("/ip/hotspot/ip-binding/remove/0");

   $READ = $API->read(false);
   $ARRAY = $API->parse_response($READ);
   print_r($ARRAY);
   $API->disconnect();
}
?>
but don't work.

I try also this :
 <?php

require('router_class.api.php');
$API = new routeros_api();
$API->debug = true;
if ($API->connect('1.1.1.1,'xxxxxx','xxxxxx')) {
   $API->comm("/ip/hotspot/ip-binding/remove", array ( 

	"numbers" => "0" ,

));

   $READ = $API->read(false);
   $ARRAY = $API->parse_response($READ);
   print_r($ARRAY);
   $API->disconnect();
}
?>  
without success.

Any suggestions ?

Bye

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

Re: RouterOs php api .

Sat May 25, 2013 12:16 am

You need to first do a "print", and get the ID of the entry you want to remove, and only then pass that ID in the remove's "numbers" argument (same as your second example, but with the ID instead of "0").
 
brasileottanta
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Thu Jun 22, 2006 8:52 am

Re: RouterOs php api .

Sat May 25, 2013 1:12 am

You need to first do a "print", and get the ID of the entry you want to remove, and only then pass that ID in the remove's "numbers" argument (same as your second example, but with the ID instead of "0").
Tthanks for replay.

 <?php

$ip_host=$_POST['ip-host'];


require('router_class.api.php');
$API = new routeros_api();
$API->debug = true;
 if ($API->connect('1.1.1.1','xxxxxx','xxxxxx')) {
 $remove = $API->comm("/ip/hotspot/ip-binding/print" , array ( 
         ".proplist" =>".id",
         "?address" => $ip_host,
));


   
$API->comm("/ip/hotspot/ip-binding/remove", array ( 

	"numbers" => $remove.id ,

));

   $READ = $API->read(false);
   $ARRAY = $API->parse_response($READ);
   print_r($ARRAY);
   $API->disconnect();
}
?> 
I try this but have also problem ...

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

Re: RouterOs php api .

Sat May 25, 2013 1:50 am

The syntax for getting array members (and that's what comm() returns - an array of arrays) is with "[]", not ".".

So replace
$remove.id
with
$remove[0]['.id']
 
brasileottanta
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Thu Jun 22, 2006 8:52 am

Re: RouterOs php api .

Sat May 25, 2013 2:12 am

The syntax for getting array members (and that's what comm() returns - an array of arrays) is with "[]", not ".".

So replace
$remove.id
with
$remove[0]['.id']

Thanks a lot !!!!

I see that you use Pear2 Php , I use RouterOs api php . What is the main difference ?

Bye :-)

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

Re: RouterOs php api .

Sat May 25, 2013 2:58 am

First stop, I must note that despite the name, having PEAR2 itself is not required.


The difference is in syntax, convenience features (many "small" ones, too many to just enumerate; and I have more in store for the upcoming version...) and support for shell-like syntax, so you don't need to know the API protocol in details.

For example, with that client, an equivalent of your code would be:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$ip_host=$_POST['ip-host'];

$client = new RouterOS\Client('1.1.1.1', 'xxxxxx', 'xxxxxx');

$id = $client->sendSync(
    new RouterOS\Request('/ip hotspot ip-binding print .proplist=.id', RouterOS\Query::where('address', $ip_host))
)->getArgument('.id');

$removeRequest = new RouterOS\Request('/ip hotspot ip-binding remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest); 
And in the upcoming version (scheduled to be released in a week or so...*), it would be even easier, like:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

$ip_host=$_POST['ip-host'];

$client = new RouterOS\Client('1.1.1.1', 'xxxxxx', 'xxxxxx');
$util = new RouterOS\Util($client);

$util->changeMenu('/ip hotspot ip-binding');
$util->remove(0);//If you know the exact position
$util->remove(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above       
* Now released.
Last edited by boen_robot on Sun Aug 18, 2013 2:04 am, edited 3 times in total.
 
brasileottanta
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Thu Jun 22, 2006 8:52 am

Re: RouterOs php api .

Sat May 25, 2013 10:47 am

First stop, I must note that despite the name, having PEAR2 itself is not required.

In fact, the syntax is very simple and logical. It was some time since I programmed and I must say that the syntax of these languages ​​is strange! :-)

Thanks again, I'll try PEAR2_Net_RouterOS.


brasileottanta
 
tapsin
just joined
Posts: 6
Joined: Wed Feb 05, 2014 6:56 pm

Re: RouterOs php api .

Wed Feb 05, 2014 7:04 pm

I'M from turkey and same english sory help me :(

1-)
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
Hi my problem php phar load class error..


and
2-)
$util->remove(0);//If you know the exact position
$util->remove(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above

or remove change disable success ?

$util->disable(0);//If you know the exact position
$util->disable(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above



First stop, I must note that despite the name, having PEAR2 itself is not required.


The difference is in syntax, convenience features (many "small" ones, too many to just enumerate; and I have more in store for the upcoming version...) and support for shell-like syntax, so you don't need to know the API protocol in details.

For example, with that client, an equivalent of your code would be:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';

$ip_host=$_POST['ip-host'];

$client = new RouterOS\Client('1.1.1.1', 'xxxxxx', 'xxxxxx');

$id = $client->sendSync(
    new RouterOS\Request('/ip hotspot ip-binding print .proplist=.id', RouterOS\Query::where('address', $ip_host))
)->getArgument('.id');

$removeRequest = new RouterOS\Request('/ip hotspot ip-binding remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest); 
And in the upcoming version (scheduled to be released in a week or so...*), it would be even easier, like:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';

$ip_host=$_POST['ip-host'];

$client = new RouterOS\Client('1.1.1.1', 'xxxxxx', 'xxxxxx');
$util = new RouterOS\Util($client);

$util->changeMenu('/ip hotspot ip-binding');
$util->remove(0);//If you know the exact position
$util->remove(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above        
* Now released.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: RouterOs php api .

Thu Feb 06, 2014 5:08 pm

1-)
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
Hi my problem php phar load class error..
Have you actually downloaded that file? You've probably downloaded the "b4.phar", in which case, it should go without saying that you must replace
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
with
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';
And also... do you have PHP 5.3.0 or later? If you are not sure, create a new PHP file with
<?php phpinfo(); ?>
and see what it says at the top.
2-)
$util->remove(0);//If you know the exact position
$util->remove(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above

or remove change disable success ?

$util->disable(0);//If you know the exact position
$util->disable(RouterOS\Query::where('address', $ip_host));//If we're talking an exact equivalent to the above
If you mean "Can you change 'remove' to 'disable' to disable matching items instead of removing them" - Yes.

Who is online

Users browsing this forum: No registered users and 5 guests