Hi.
It’s an order of operations thing. You’re getting the request’s argument, not the responses’.
Perhaps it may make more sense if we rewrite the working example a little bit to include more variables:
$srequest = new RouterOS\Request('/interface/ethernet/monitor numbers=ether1 once');
$responses = $client->sendSync($srequest);
$rate = $responses->getArgument('rate');
echo 'RATE:',$rate;
What happens:
- Prepare a request.
- Send a request.
- Out of the first response, get the attribute “rate”.
- Write out its value.
The code you’re trying to run is instead equivalent to:
$srequest = new RouterOS\Request('/interface/ethernet/monitor numbers=ether1 once');
$rateArg = $srequest->getArgument('rate');
$results = $client->sendSync($rateArg);
echo 'bps:',$rate;
i.e.
- Prepare a request.
- Get the “rate” argument of the request. There’s none, so you get NULL.
- Send the value of the “rate” argument, i.e. send NULL… Which is not allowed.
- Write out the variable $rate, which BTW is not existent at all (not that it matters, since the script has already terminated due to 3).
In your second code, you’re doing a similar thing wrong, except it’s even worse, in that you’re trying to get two arguments with one getArgument() call, which is not allowed. Plus, the second one is written out as a series of non-existent constants.
$client->sendSync($trequest->getArgument('rx-bits-per-second',tx-bits-per-second));
- In item 2 , I wish to receive the value of rx-bits-per-second’ and tx-bits-per-second continuously.
I try to make a graph for long term monitoring. please advise how to get the value continuously in PHP.
It’s possible to request the correct coding.
As my understand , PHP web will be idle timeout after 2-3 minute .
In that case, you need to EITHER remove the “once” argument, and use sendAsync() instead of sendSync() and write out values continuously OR call the page many times with ajax, with each having a “once” argument.
The former is more efficient, but requires a lot of things on both client side and server side to come together for it to work properly, so I’ll just demonstrate the simpler option.
$trequest = new RouterOS\Request('/interface monitor-traffic=ether1 once');
$result = $client->sendSync($trequest)->current();
echo json_encode(
array(
'rx' => $result('rx-bits-per-second'),
'tx' => $result('tx-bits-per-second')
)
);
You call the PHP file with that code once every second or however often you need to refresh the value, and parse it using the browser’s JSON.parse() method, and then do what you will with it.
To avoid similar sorts of confusion in the future, I’ve been meaning to make a change in the API client, which I’ve so far been a little hesitant to do, since it’s a breaking change (i.e. some people will need to change their code). I’ve adjusted the code above so that you’re still OK in both the current and new versions. The change in question - rename Response::getArgument() to getProperty(), and only keep getArgument() on requests, making it clear whether you’re dealing with the request or response.