1. Set up the environment
1.1. Set up a web server with PHP 5.3.0 or higher (ideally, the latest PHP version; currently 5.4.14).
1.2. Open a RouterOS terminal (e.g. from Winbox, the "New Terminal" button), type
1.3. On your web server's firewall, allow the php-cgi executable and/or your web server's executable (e.g. httpd.exe for Apache on Windows) to make outgoing connections. How to do this depends on your OS and firewall software.
2. Install the client.
2.1. Click the link in my signature.
2.2. Click the icon with "PHAR" on it to download a ".phar" file with the client.
2.3. Place the file anywhere on your web server, as long as it is accessible from PHP. If you're new to PHP, you'll want to place the file in the same folder as the PHP file you'll be creating in a moment.
3. Create the PHP file(s) that will connect to the router.
3.1. Include the ".phar" file and create a Client object. Assuming the ".phar" file is in the same folder as the PHP file in question, have the following code first, as a test run:
<?php
use PEAR2\Net\RouterOS;
echo 'Parsing is OK if you are seeing this...';
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
echo 'File included!...';
try {
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
echo 'Connection OK!';
} catch (Exception $e) {
echo 'Connection FAIL! Details:', $e;
}
(NOTE: Adjust your RouterOS IP, username and password)
3.2. Run your test file. The example above should output
Parsing is OK if you are seeing this...File included!...Connection OK!
If that's what you're seeing, you can move on to the real deal.
4. Doing stuff to RouterOS.
4.1a Here's an example file that adds a new concrete user, with a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
try {
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$addRequest = new RouterOS\Request('/ip hotspot user add profile=default name=username password=hotspotPassword');
$client($addRequest);
} catch (Exception $e) {
echo $e;
}
4.1b Here's a a more elaborate example file that add a new user with a username and password based on fields in a form, and a concrete profile called "default":
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
header('Content-Type: text/html;charset=UTF-8');
$errors = array();
if (isset($_POST['username']) && isset($_POST['password'])) {
try {
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$addRequest = new RouterOS\Request('/ip hotspot user add profile=default');
$addReques
->setArgument('name', $_POST['username'])
->setArgument('password', $_POST['password']);
$errorResponses = $client($addRequest)->getAllOfType(RouterOS\Response::TYPE_ERROR);
foreach ($errorResponses as $error) {
$errors[] = $error->getArgument('message');
}
} catch (Exception $e) {
$errors[] = $e;
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Add new user</title>
<style type="text/css">
#OK {background-color:darkgreen;color:white;}
#errors {background-color:darkred;color:white;}
</style>
</head>
<body>
<form action="" method="post">
<?php
if (!empty($_POST)) {
if (empty($errors)) {
echo '<div id="OK">User added successfully</div>';
} else {
echo '<div id="errors"><ol>';
foreach ($errors as $error) {
echo '<li>', $error, '</li>';
}
echo '</ol></div>';
}
}
?>
<div>
<ol>
<li>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="" />
</li>
<li>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="" />
</li>
<li>
<input type="submit" value="Add" />
</li>
</ol>
</div>
</form>
</body>
</html>
If you're seeing the pattern here, doing anything from this point on should be relatively easy. The harder part is the UI, session maintenance, and other common difficulties with any more complex PHP application.