Page 1 of 1

PHP API =follow=1

Posted: Sat Dec 08, 2012 10:39 am
by omidkosari
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

Re: PHP API =follow=1

Posted: Sat Dec 08, 2012 4:16 pm
by boen_robot
"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   

Re: PHP API =follow=1

Posted: Mon Dec 24, 2018 12:24 pm
by tva94
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();
?>

Re: PHP API =follow=1

Posted: Mon Dec 24, 2018 4:03 pm
by boen_robot
If you mean how do you make completeResponse() work... you just don't call loop().