Page 1 of 1

RouterOs php api .

Posted: Fri May 24, 2013 10:15 pm
by brasileottanta
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

Re: RouterOs php api .

Posted: Sat May 25, 2013 12:16 am
by boen_robot
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").

Re: RouterOs php api .

Posted: Sat May 25, 2013 1:12 am
by brasileottanta
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

Re: RouterOs php api .

Posted: Sat May 25, 2013 1:50 am
by boen_robot
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']

Re: RouterOs php api .

Posted: Sat May 25, 2013 2:12 am
by brasileottanta
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

Re: RouterOs php api .

Posted: Sat May 25, 2013 2:58 am
by boen_robot
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.

Re: RouterOs php api .

Posted: Sat May 25, 2013 10:47 am
by brasileottanta
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

Re: RouterOs php api .

Posted: Wed Feb 05, 2014 7:04 pm
by tapsin
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.

Re: RouterOs php api .

Posted: Thu Feb 06, 2014 5:08 pm
by boen_robot
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.