A ReactPHP-based client for the Ubiquiti Unifi Controller API. This library enables asynchronous communication with Unifi Controllers using ReactPHP.
- PHP 8.1 or higher
- ReactPHP HTTP Client
composer require skydiablo/reactphp-unifi-api-client
<?php
use React\EventLoop\Loop;
use SkyDiablo\UnifiApiClient\UnifiClient;
use SkyDiablo\UnifiApiClient\Services\BasicService;
use SkyDiablo\UnifiApiClient\Services\Site;
use SkyDiablo\UnifiApiClient\Services\Device;
// Create client
$client = new UnifiClient(
'https://unifi.example.com:8443',
'username',
'password'
);
// Create services
$basicService = new BasicService($client);
$siteService = new Site($client);
$deviceService = new Device($client);
// Get controller information
$basicService->getInfo()->then(function (array $info) {
echo "Controller Version: " . $info['version'] . "\n";
});
// Get sites
$siteService->getSites()->then(function (array $sites) {
foreach ($sites as $site) {
echo "Site: " . $site['name'] . " (" . $site['desc'] . ")\n";
}
});
// Get devices
$deviceService->getDeviceBasics()->then(function (array $devices) {
foreach ($devices as $device) {
echo "Device: " . ($device['name'] ?? 'Unnamed') . " (" . $device['mac'] . ")\n";
}
});
// Logout when finished
$client->logout()->then(function () {
echo "Logged out\n";
Loop::stop();
});
// Start event loop
Loop::run();
getInfo()
: Retrieves information about the controller
getSites()
: Retrieves all available sites
getDeviceBasics(string $site = 'default')
: Retrieves basic information about devices at a sitegetDevicesV2(string $site = 'default', bool $separateUnmanaged = false, bool $includeTrafficUsage = false)
: Retrieves detailed device information (v2 API)
All API requests return a Promise object. Use the catch
method to handle errors:
$client->login()->then(
function () {
echo "Login successful\n";
},
function (\Exception $e) {
echo "Login failed: " . $e->getMessage() . "\n";
}
);
The project includes a comprehensive test suite with unit tests and integration tests. For more information, see the Test Documentation.
Contributions are welcome! Please ensure your changes pass the tests and add new tests as needed.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT