API PHP Class
API PHP Class
Note: This is just one of three PHP API clients. Check out also the API clients by Vasil
Rangelov and Kamil Trzcinski
Contents
[hide]
1 Info
o 1.1 Source
o 1.2 Author
o 1.3 Contributors
o 1.4 Changelog
2 Examples
o 2.1 Example 1
2.1.1 Output
o 2.2 Example 2
2.2.1 Output
o 2.3 Example 3
o 2.4 Example 4
o 2.5 Example 5
3 See also
Info
This is PHP class for working with RouterOS API. You can take it, edit it and use it as you need.
Warning: The class as shown does not work for large replies
Source
Note: Source has been removed from article and moved to the location specified below
Download, view, and contribute to the source at https://github.com/BenMenking/routeros-api
Author
Denis Basta (Denis [dot] Basta [at] gmail [dot] com)
Contributors
Nick Barnes
Ben Menking (ben [at] infotechsc [dot] com)
Jeremy Jefferson (http://jeremyj.com)
Cristian Deluxe (djcristiandeluxe [at] gmail [dot] com)
Mikhail Moskalev (mmv.rus [at] gmail [dot] com)
Changelog
1.0 Denis Basta (Denis [dot] Basta [at] gmail [dot] com) First PHP Class released from author
1.1 Nick Barnes
read() function altered to take into account the placing of the "!done" reply and also correct
calculation of the reply length.
Examples
Example 1
<?php
require('routeros_api.class.php');
$API->debug = true;
$API->write('/interface/getall');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
?>
OR
<?php
require('routeros_api.class.php');
$API->debug = true;
print_r($ARRAY);
$API->disconnect();
?>
OR
<?php
require('routeros_api.class.php');
$API->debug = true;
$ARRAY = $API->comm('/interface/getall');
print_r($ARRAY);
$API->disconnect();
?>
Output
Array
(
[0] => Array
(
[.id] => *1
[name] => ether1
[mtu] => 1500
[type] => ether
[running] => yes
[dynamic] => no
[slave] => no
[comment] =>
[disabled] => no
)
Example 2
Thanks a lot for this API, It help me a lot to write my php page for our support team to have
access to wireless registration table.
$API->write('/interface/wireless/registration-table/print',false);
$API->write('=stats=');
Output
Array
(
[0] =>
Array
(
[.id] => *147
[comment] =>
[interface] => AP101
[mac-address] => 00:0B:6B:37:58:33
[ap] => false
[wds] => false
[rx-rate] => 11Mbps
[tx-rate] => 11Mbps
[packets] => 4043966,2669114
[bytes] => 3961713942,280551024
[frames] => 4043966,2669114
[frame-bytes] => 3937477458,264536340
[hw-frames] => 4500839,2669114
[hw-frame-bytes] => 256326637,349947988
[tx-frames-timed-out] => 0
[uptime] => 1w13:09:12
[last-activity] => 00:00:00.090
[signal-strength] => -73dBm@11Mbps
[signal-to-noise] => 30
[strength-at-rates] => -73dBm@1Mbps 4m4s640ms,-73dBm@2Mbps
4m58s730ms,-73dBm@5.5Mbps 42s450ms,-73dBm@11Mbps 90ms
[tx-ccq] => 91
[p-throughput] => 5861
[ack-timeout] => 31
[last-ip] => 192.168.0.220
[802.1x-port-enabled] => true
[wmm-enabled] => false
)
[1] => Array
(
...
)
...
)
Example 3
Adding vpn user
$API->comm("/ppp/secret/add", array(
"name" => "user",
"password" => "pass",
"remote-address" => "172.16.1.10",
"comment" => "{new VPN user}",
"service" => "pptp",
));
Example 4
Find registration-table id for specified MAC
$ARRAY = $API->comm("/interface/wireless/registration-table/print",
array(
".proplist"=> ".id",
"?mac-address" => "00:0E:BB:DD:FF:FF",
));
print_r($ARRAY);
Example 5
Count leases from specific IP Pool (using regexp count all IPs starting with 1.1.x.x)
or
$API->write('/ip/dhcp-server/lease/print', false);
$API->write('=count-only=', false);
$API->write('~active-address~"1.1."');
$ARRAY = $API->read();
print_r($ARRAY);
See also