/your_project_folder
│── /routeros-api <– Cloned RouterOS API (BenMenking/routeros-api)
│ ├── routeros_api.class.php
│ ├── README.md
│── /config
│ ├── db.php
│── /admin
│ ├── manage_routers.php
│ ├── add_router.php
│ ├── edit_router.php
│── index.php
│── test_mikrotik.php
Step 1: Include the API in Your PHP Files
You need to include the RouterOS API class in every PHP file where you want to interact with the Mikrotik router.
✅ Method 1: Direct Include
Add the following line at the beginning of your PHP files where you need Mikrotik API access:
require_once __DIR__ . '/../routeros-api/routeros_api.class.php';
- If your file is inside the
admin
folder (e.g.,manage_routers.php
), use:require_once __DIR__ . '/../routeros-api/routeros_api.class.php'
- If your file is in the root directory (e.g.,
index.php
), use: phpCopyEditrequire_once 'routeros-api/routeros_api.class.php';
Step 2: Connect to a Mikrotik Router
Create a new file config/mikrotik.php
to handle the API connection:
config/mikrotik.php
phpCopyEdit<?php
require_once __DIR__ . '/../routeros-api/routeros_api.class.php';
class MikrotikAPI {
private $API;
private $router_ip;
private $username;
private $password;
public function __construct($router_ip, $username, $password) {
$this->API = new RouterosAPI();
$this->router_ip = $router_ip;
$this->username = $username;
$this->password = $password;
}
public function connect() {
return $this->API->connect($this->router_ip, $this->username, $this->password);
}
public function getSystemResources() {
if ($this->connect()) {
$data = $this->API->comm('/system/resource/print');
$this->API->disconnect();
return $data;
}
return false;
}
public function getActiveUsers() {
if ($this->connect()) {
$users = $this->API->comm('/ppp/active/print');
$this->API->disconnect();
return $users;
}
return false;
}
}
Step 3: Use API in manage_routers.php
Now modify manage_routers.php
to display router data.
admin/manage_routers.php
phpCopyEdit<?php
session_start();
require_once '../config/db.php'; // Database connection
require_once '../config/mikrotik.php'; // Mikrotik API Connection
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
header("Location: ../auth/login.php");
exit();
}
// Fetch routers from the database
$query = "SELECT * FROM routers ORDER BY id DESC";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h1>Mikrotik Router Management</h1>
<a href="add_router.php" class="btn btn-success">Add Router</a>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Router Name</th>
<th>Router IP</th>
<th>Username</th>
<th>Location</th>
<th>System Info</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$count = 1;
while ($row = mysqli_fetch_assoc($result)) {
// Connect to the router and fetch system info
$mikrotik = new MikrotikAPI($row['router_ip'], $row['username'], $row['password']);
$sysInfo = $mikrotik->getSystemResources();
echo "<tr>
<td>{$count}</td>
<td>{$row['router_name']}</td>
<td>{$row['router_ip']}</td>
<td>{$row['username']}</td>
<td>{$row['location']}</td>
<td>";
if ($sysInfo) {
echo "CPU Load: " . $sysInfo[0]['cpu-load'] . "%, Uptime: " . $sysInfo[0]['uptime'];
} else {
echo "<span class='text-danger'>Failed to connect</span>";
}
echo "</td>
<td>
<a href='edit_router.php?id={$row['id']}' class='btn btn-primary btn-sm'>Edit</a>
<a href='delete_router.php?id={$row['id']}' class='btn btn-danger btn-sm' onclick='return confirm(\"Are you sure?\")'>Delete</a>
</td>
</tr>";
$count++;
}
?>
</tbody>
</table>
</div>
<?php
$conn->close();
?>
Step 4: Test the API
1️⃣ Test Router Connection:
Create a test file test_mikrotik.php
in your project root:
phpCopyEdit<?php
require_once 'routeros-api/routeros_api.class.php';
$API = new RouterosAPI();
$router_ip = "192.168.88.1";
$username = "admin";
$password = "yourpassword";
if ($API->connect($router_ip, $username, $password)) {
echo "Connected Successfully!";
$response = $API->comm('/system/resource/print');
print_r($response);
$API->disconnect();
} else {
echo "Failed to connect!";
}
?>
Run it using:
shCopyEditphp test_mikrotik.php
If successful, it will print the router’s system information.
✅ Summary
- Include API in your PHP project: phpCopyEdit
require_once 'routeros-api/routeros_api.class.php';
- Store routers in your database (
routers
table). - Use
MikrotikAPI
class to connect and retrieve data. - Display router info in your admin panel (
manage_routers.php
). - Test the connection with
test_mikrotik.php
.
🚀 Next Steps
- ✅ Add functions to create, update, and delete users in Mikrotik.
- ✅ Integrate PPPoE user management.
- ✅ Automate MAC binding & router settings.
Source : https://chatgpt.com/share/67d2539d-acbc-800d-a5d7-4669ea0596d8
Leave a Reply