Community discussions

 
User avatar
omidkosari
Trainer
Trainer
Topic Author
Posts: 616
Joined: Fri Sep 01, 2006 4:18 pm
Location: Iran , Karaj
Contact:

PHP API =follow=1

Sat Dec 08, 2012 10:39 am

according to this topic http://forum.mikrotik.com/viewtopic.php?f=2&t=34792 i want to run the following code in php
/ip/firewall/address-list/print
=follow=1
.tag=adr
but php will not pass the last line of
	$API->write('/ip/firewall/address-list/print', false);
	$API->write("=follow=1", false);
	$API->write('.tag=adr');
	$API->read(true);
also i tried async request in the newer php class
$addRequest = new Request('/ip/firewall/address-list/print');
 
$addRequest->setArgument('follow', '1');
$addRequest->setTag('adr');
$client->sendAsync($addRequest);
 
it has same problem . the last line will not finish . i want an always running code and use the data received from router .
each time something adds or removes from specified address-lists the script should do the same in another router in near realtime
MTCNA , MTCRE, MTCWE, Mikrotik Certified Trainer
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP API =follow=1

Sat Dec 08, 2012 4:16 pm

"follow" turns "print" into an interactive command, and interactive commands are not available over the API protocol.

To do what you want, you can use the "listen" command instead, e.g.
$listenRequest = new Request('/ip/firewall/address-list/listen');
 
$listenRequest->setTag('adr');
$client->sendAsync($listenRequest); 
It's important to note however, that with both PHP clients currently, if your connection remains idle for default_socket_timeout seconds (i.e. you start receiving with loop() or completeRequest(), but no data arrives in that time), it will be terminated.

I plan to change that in my client later on, but in the meantime, as a workaround, you can put out a ping command that runs every "default_socket_timeout - 1" seconds, thus avoiding the disconnect, e.g.
$listenRequest = new Request('/ip/firewall/address-list/listen');
 
$listenRequest->setTag('adr');
$client->sendAsync($listenRequest);

$pingRequest = new Request('/ping address=127.0.0.1 .proplist', null, 'p');
$pingRequest->setArgument('interval', ini_get('default_socket_timeout') - 1);
$client->sendAsync($pingRequest);

//execute loop() or completeRequest() here   
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
tva94
just joined
Posts: 1
Joined: Mon Dec 24, 2018 12:04 pm

Re: PHP API =follow=1

Mon Dec 24, 2018 12:24 pm

UPD: As for me next code works, but what other variants?

Code: Select all


<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2/Autoload.php';

$responseHandler = function ($response) {
var_dump($response);
};

try {
$client = new RouterOS\Client('192.168.88.1', 'api', 'api');
} catch (Exception $e) {
die('Unable to connect to the router. ' . $e->getMessage());
}

$client->setStreamingResponses(true);

$aclListenRequest = new RouterOS\Request('/interface/wireless/access-list/listen');
$aclListenRequest->setTag('tag1');
$client->sendAsync($aclListenRequest, $responseHandler);
$client->loop();

//foreach ($client->completeRequest('tag1') as $response) {
// var_dump($response);
//}

$client->close();
?>
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: PHP API =follow=1

Mon Dec 24, 2018 4:03 pm

If you mean how do you make completeResponse() work... you just don't call loop().
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: No registered users and 6 guests