Mikrotik PHP-API

As I was checking out the API I made a small interface for monitoring one router.

I added 5 menus:
1.Resources
2.Basic list of interfaces
3.Wireless Interfaces
4.Registration Table
5.Hotspot/Hosts


All you need to do is change the ip/user/pwd of your RB in the 5 php files (and ofcourse open port for the api in /ip/services/ in your RB).-Sorry you have to do that in 5 sections and not in one php file… :slight_smile:

The values (like in.ex. signal strength) are not stored in a database as an array.They are just displayed dynamically as a user requests the php files.
This uses the php api class in the wiki:
http://wiki.mikrotik.com/wiki/API_PHP_class
I also used overLIB popup JavaScript library (it’s included in the rar) ,merging some values together, because the tables where way to long for my screen sometimes.

Hope you like it.This is as simple as it gets (basic hmtml,php,javascript) but shows how easy it is to get the values and make an interface.
mikrotik-php-api.rar (123 KB)

I have a few comments I would like to post for anyone with the php knowledge who wants to deal with it:
Let’s sat we want to use php-api as a display sollution.

The above api-php-class works even in a shared hosting environment (tested it and works very well)
This is because of the brilliant use of fsockopen in the php-class -regards to the user who wrote that! :sunglasses: which lets a shared hosting environment php script call mikrotik in port 8728.
Even though it’s not really secure in a shared hosting to call other port than 80 it’s easy to use and you can provide information (let’s say about a hotspot, or signals - I don’t suggest providing mac addresses of your users :open_mouth: even though I added every value you can just erase in the table the columns of sensitive values) in a http://www.domain.com . It also works on dyndns hosts as long as your modem redirects the port 8728 to your routerboard.

Things simplified are:
If you are in a shared hosting:
1.Upload , let’s say, total users connected php in http://www.domain.com
2.Users visit your domain viewing php script
3.php script calls mikrotik api
4.php returning array of values for what u asked (command /interface/registration-table/print) and php displays in your domain a value from that array (which in this case is the number of total users connected to your mikrotik).

Now if you want to monitor the above number through time (let’s say that you want to show a graph of total users in you domain.com) you would need an array of values for total users through time.
That’s why you would propably need a cron job running the php script every 15min or so, a database to store those values every 15min, and a php graph script like the “PHPGraphLib” found here http://www.ebrueggeman.com/phpgraphlib/examples.php to display/graph those values from your database.

I know it’s a lot and it’s more like a php question but if done provides a way of graphing externally (even in shared hosting) any value that can be retrieved with the mikrotik-api.Just a thought but if anyone having the php knowledge and the time would like to try it would be great.

PS:I need coffee… :open_mouth:

Hi folks. What’s up?!
That is really an amazing and useful PHP class that I’ve been using a lot in my projects.

Well.. I only got this simple question about it.
After executing a script that uses the routeros_api.class.php class, I got this message:

Notice: Undefined variable: _ in ... on line 313

Then, I would like to ask you guys a question:
First, consider the following piece of code which exists in that php class (lines 300 up to 320):

         // If we have got more characters to read, read them in.
         if ($LENGTH > 0) {
            $_ = "";
            $retlen=0;
            while ($retlen < $LENGTH) {
               $toread = $LENGTH - $retlen ;
               $_ .= fread($this->socket, $toread);
               $retlen = strlen($_);
            }
            $RESPONSE[] = $_ ;
            $this->debug('>>> [' . $retlen . '/' . $LENGTH . ' bytes read.');
         }

         // If we get a !done, make a note of it.
         if ($_ == "!done")
            $receiveddone=true;

         $STATUS = socket_get_status($this->socket);

         
         if ($LENGTH > 0)
            $this->debug('>>> [' . $LENGTH . ', ' . $STATUS['unread_bytes'] . '] ' . $_);

So, there is this PHP variable $_ on it.
Besides the code above, I’ve glanced the entire class and saw no other mentions for that $_
I suppose my script don’t execute those instructions inside that first conditional, i.e., if ($LENGTH > 0) …
Therefore, when the script reachs that second conditional, i.e., if ($_ == “!done”), the PHP produces the notice message

undefined variable: _

, just because the $_ variable had never been initialized.
Although it’s just a simple notice message, is that code right? I mean, don’t you think the $_ variable should be initialized somewhere before the execution of those IF’s?
That’s it.
Thanks for any reply.

rod~

Can you share any of them?

I think you should post for a solution where the api class was originally posted in scripts. It wasn’t written by me but just so you know I’ve never had this kind of message.I belive the variable is set (at first time empty) in:

$_ = "";

it might be a register_globals issue. Turn it on and see if you still have the same errors.Or if on shared hosting prob. you can’t change much.

Hi sw0rdf1sh

Thanks for replying me.

Can you share any of them?

Sure! I could mention some simple stuffs, like adding MAC Addresses in ‘Wireless Tables → Access List’.
Also, adding and removing simple queues for my users.
And mainly, adding and removing ‘Address List’ in the Mikrotik’s Firewall section.

I think you should post for a solution where the api class was originally posted in scripts. It wasn’t written by me but just so you know I’ve never had this kind of message.

I see. As a matter of fact, I’m posting this same question on this thread here:

http://forum.mikrotik.com/t/api-php-class/22551/1

Maybe I could get some answer.

I belive the variable is set (at first time empty) in:

$_ = “”;

>

I understand. However, by reading that code, if my first 'if ()' evaluates to a false value, then my $_ won't be set. This might cause that notice message as mentioned earlier.

> It might be a register_globals issue.

Unfortunately, I can't change that option.

Thanks bro!

rod~

can not use


cada quien tiene su manera de programar yo e tomado el codigo y e corregido de esta forma trabaja muy bien

Hotspot/Hosts in Mikrotik <?php function popup( $text, $popup ){ ?> <?php echo($text); ?> <?php } ?> <?php require('routeros_api.class.php'); $API = new routeros_api(); $API->debug = false; $API->port = 2086; $servidor="172.16.0.1"; $usuario="admin"; $clave="88708870"; if ($API->connect($servidor, $usuario, $clave)) { $ARRAY = $API->comm("/ip/hotspot/host/print"); $registros=count($ARRAY); echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; if ($regtable['DHCP']=="true") { echo ""; }else{ echo "";

}
if ($regtable[‘authorized’]==“true”) {
echo “

”;
}else{
echo “”;
}
if ($regtable[‘bypassed’]==“true”) {
echo “”;
}else{
echo “”;
}
echo “”;
echo “”;
}
echo “
Idmac-addressaddressto-addressserveruptimekeepalive-timeoutfound-byDHCPauthorizedbypassedcomment
"; for ($i=0; $i<$registros; $i++){ $regtable = $ARRAY[$i]; // if (!isset($regtable['.id'])) { $regtable['.id']="";} if (!isset($regtable['mac-address'])) { $regtable['mac-address']="";} if (!isset($regtable['address'])) { $regtable['address']="";} if (!isset($regtable['to-address'])) { $regtable['to-address']="";} if (!isset($regtable['server'])) { $regtable['server']="";} if (!isset($regtable['uptime'])) { $regtable['uptime']="";} if (!isset($regtable['keepalive-timeout'])) { $regtable['keepalive-timeout']="";} if (!isset($regtable['found-by'])) { $regtable['found-by']="";} if (!isset($regtable['DHCP'])) { $regtable['DHCP']="";} if (!isset($regtable['authorized'])) { $regtable['authorized']="";} if (!isset($regtable['bypassed'])) { $regtable['bypassed']="";} if (!isset($regtable['comment'])) { $regtable['comment']="";} // echo "
" . $regtable['.id'] . "" . $regtable['mac-address'] . "" . $regtable['address'] . "" . $regtable['to-address'] . "" . $regtable['server'] . "" . $regtable['uptime'] . "" . $regtable['keepalive-timeout'] . "" . $regtable['found-by'] . "" . $regtable['DHCP'] . "" . $regtable['DHCP'] . "” . $regtable[‘authorized’] . “” . $regtable[‘authorized’] . “” . $regtable[‘bypassed’] . “” . $regtable[‘bypassed’] . “” . $regtable[‘comment’] . “
”;
$API->disconnect();
}
?>

Hello to all,
It’s really a good class. I tested it as downloaded.
Yet I have a question, if I want to use it, let’s say for example to edit things in the router like registering new users from the hotspot login page, how can I proceed.

I have been trying this
$d = [‘name’=‘ob’, ‘password’=‘Passpass’, ‘profile’=‘default’];

$ARRAY = $API->comm(“/ip/hotspot/user/add”, $d);

But nothing happens, all I see in the logs is the connection of the user i provided via api, but no new user is created in the hotspot users table.
Can someone help me please ?