API call all in one

Hi,
actually, I’m doing different API calls to get information from the router like:

$binding=$this->util->setMenu(‘/ip/hotspot/ip-binding’)->getAll();
$hotspots=$this->util->setMenu(‘/ip/hotspot’)->getAll();
$routerInfo=$this->util->setMenu(‘/interface/wireless’)->getAll();

At the end I call the router 4 or 5 times and some routers are really far away so it can take 3 or 4 seconds to get this information.

Is there any way to group all the calls in one call and get the results in an array per call or something like that ?

Thanks

You could use client’s sendAsync() instead of the util methods.

After making all sendAsync() calls, make a sequence of completeRequest() calls for each request, e.g.

$client->sendAsync(new RouterOS\Request('/ip/hotspot/ip-binding/print', null, 'bindings'));
$client->sendAsync(new RouterOS\Request('/ip/hotspot/print', null, 'hotspots'));
$client->sendAsync(new RouterOS\Request('/interface/wireless/print', null, 'wireless'));

$binding = $client->completeRequest('bindings')->getAllOfType(RouterOS\Response::TYPE_DATA);
$hotspot = $client->completeRequest('hotspots')->getAllOfType(RouterOS\Response::TYPE_DATA);
$wireless = $client->completeRequest('wireless')->getAllOfType(RouterOS\Response::TYPE_DATA);

The total amount of data you send and receive using this approach is a few bytes more than with util, but should come in a bit faster, because everything will be sent in one go, and then received in (almost) one go.

If you’re doing many other things within the same request, I would recommend you place your sendAsync() calls as early as possible, and the completeRequest() calls as late as possible, and do everything else in the mean time. This should give PHP enough time to buffer the replies while you’re doing everything else.