Im trying to use this method again for another use case but am having an issue… i seem to be getting this Call to a member function getType() on a non-object. Could you lend a hand? could it be something to do with the memory buffer?
I’m assuming you’re talking about the second quoted code (since the first contains no calls to getType())?
In the latest version, Response::getArgument() was renamed to Response::getProperty(). You can make a cross-version compatible code by not using a name… That one is probably not your particular issue though…
Try to add
var_dump($response);
in the function, and see what’s there, i.e.
$client->sendAsync(
new RouterOS\Request('/tool ping-speed address=192.168.88.100 interval=0.5', null, 'p'),
function ($response) use (&$count, &$avg) {
var_dump($response);
if ($response->getType() === RouterOS\Response::TYPE_DATA) {
$avg = $response('average');
return 100 === ++$count;
}
}
);
It should be an object of type PEAR2\Net\RouterOS\Response…
Also, make sure the variable name is the same (copy&paste the one on the “function” line to the “getType()” line; just in case).
If you mean stopping execution of the callback at a point, then you can, by returning true from within the callback. The callback will then be executed only 2 times more - once with an error reply, and once more with a last reply (signaling there’s not going to be anything more).
If you mean returning a value from inside the callback to the outside, that’s what the “use” part after the function is. You add a variable by reference, and then any modifications inside the function will be available from the outside too.
But yes, PHP doesn’t work asynchronously. For a workaround, you’d need to install the pthreads extension, and invoke a thread for each whatever. The API client is not thread safe though, so if you’re using the same connection in multiple threads, you’d need to lock() and wait() accordingly.