API client class v1.1.75

- updated docblocks to be less USG-specific
- removed content-length header from the logout method, reported by @Olivier6767
- added notes to reflect successful tests with UDR running latest Pre-Release version of the UniFi controller (version 6.5.52)
- added checks in specific cases when using trim() to prevent PHP 8 from throwing an error when the variable is null, submitted by @djchen
This commit is contained in:
malle-pietje
2021-12-18 12:00:56 +01:00
parent fbfd6a8246
commit aefbbdc5e1
2 changed files with 30 additions and 24 deletions

View File

@@ -2,7 +2,7 @@
A PHP class that provides access to Ubiquiti's [**UniFi Network Controller**](https://unifi-network.ui.com/) API.
UniFi Network Controller software versions 4.X.X, 5.X.X and 6.X.X are supported as well as UniFi OS-based controllers (version 6.5.50 has been confirmed to work).
UniFi Network Controller software versions 4.X.X, 5.X.X and 6.X.X are supported as well as UniFi OS-based controllers (version 6.5.55 has been confirmed to work).
This class is used by our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
The package can be installed manually or by using
@@ -12,9 +12,9 @@ easy inclusion in your projects.
## Requirements
- a server with:
- PHP version 5.5.0 or higher
- PHP 5.5.0 or higher
- PHP json and PHP cURL modules
- tested on Apache 2.4 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.2.24 and cURL 7.58.0
- tested on Apache 2.4 with PHP 5.6.1 and cURL 7.42.1 and with PHP 7.4.9 and cURL 7.68.0
- direct network connectivity between this server and the host and port (normally TCP port 8443 or port 443 for
UniFi OS) where the UniFi Controller is running
- you must use **accounts with local access**, not pure UniFi Cloud accounts, to access the UniFi Controller API through
@@ -22,7 +22,7 @@ easy inclusion in your projects.
## UniFi OS Support
Support for UniFi OS-based controllers (UniFi Dream Machine, UniFi Dream Machine Pro
Support for UniFi OS-based controllers (UniFi Dream Router, UniFi Dream Machine, UniFi Dream Machine Pro
or Cloud Key Gen2/Cloud Key Gen2 Plus with firmware version 2.0.24 or higher) has
been added as of version 1.1.47. The class automatically detects UniFi OS devices and
adjusts URLs and several functions/methods accordingly. If your own code implements strict
@@ -347,7 +347,7 @@ This class is based on the initial work by the following developers:
and the API as published by Ubiquiti:
- https://dl.ui.com/unifi/6.2.26/unifi_sh_api
- https://dl.ui.com/unifi/6.5.55/unifi_sh_api
## Important Disclaimer

View File

@@ -13,7 +13,7 @@ namespace UniFi_API;
*
* @package UniFi_Controller_API_Client_Class
* @author Art of WiFi <info@artofwifi.net>
* @version Release: 1.1.71
* @version Release: 1.1.75
* @license This class is subject to the MIT license that is bundled with this package in the file LICENSE.md
* @example This directory in the package repository contains a collection of examples:
* https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples
@@ -26,7 +26,7 @@ class Client
* NOTE:
* do not modify the values here, instead use the constructor or the getter and setter functions/methods
*/
const CLASS_VERSION = '1.1.74';
const CLASS_VERSION = '1.1.75';
protected $baseurl = 'https://127.0.0.1:8443';
protected $user = '';
protected $password = '';
@@ -100,14 +100,14 @@ class Client
public function __destruct()
{
/**
* if $_SESSION['unificookie'] is set, do not logout here
* if $_SESSION['unificookie'] is set, do not log out here
*/
if (isset($_SESSION['unificookie'])) {
return;
}
/**
* logout, if needed
* log out, if needed
*/
if ($this->is_logged_in) {
$this->logout();
@@ -247,7 +247,6 @@ class Client
* construct the HTTP request headers as required
*/
$this->curl_headers = [
'content-length: 0',
'Expect:',
];
@@ -799,7 +798,7 @@ class Client
* - this function/method is only supported on controller versions 5.5.* and later
* - make sure that the retention policy for 5 minutes stats is set to the correct value in
* the controller settings
* - requires a USG
* - requires a UniFi gateway
*
* @param int $start optional, Unix timestamp in milliseconds
* @param int $end optional, Unix timestamp in milliseconds
@@ -823,7 +822,7 @@ class Client
*
* NOTES:
* - defaults to the past 7*24 hours
* - requires a USG
* - requires a UniFi gateway
*
* @param int $start optional, Unix timestamp in milliseconds
* @param int $end optional, Unix timestamp in milliseconds
@@ -847,7 +846,7 @@ class Client
*
* NOTES:
* - defaults to the past 52 weeks (52*7*24 hours)
* - requires a USG
* - requires a UniFi gateway
*
* @param int $start optional, Unix timestamp in milliseconds
* @param int $end optional, Unix timestamp in milliseconds
@@ -871,7 +870,7 @@ class Client
*
* NOTES:
* - defaults to the past 52 weeks (52*7*24 hours)
* - requires a USG
* - requires a UniFi gateway
*
* @param int $start optional, Unix timestamp in milliseconds
* @param int $end optional, Unix timestamp in milliseconds
@@ -895,7 +894,7 @@ class Client
*
* NOTES:
* - defaults to the past 24 hours
* - requires a USG
* - requires a UniFi gateway
*
* @param int $start optional, Unix timestamp in milliseconds
* @param int $end optional, Unix timestamp in milliseconds
@@ -909,13 +908,12 @@ class Client
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/archive.speedtest', $payload);
}
/**
* Fetch IPS/IDS events
*
* NOTES:
* - defaults to the past 24 hours
* - requires a USG
* - requires a UniFi gateway
* - supported in UniFi controller versions 5.9.X and higher
*
* @param int $start optional, Unix timestamp in milliseconds
@@ -1039,13 +1037,17 @@ class Client
*/
public function list_clients($client_mac = null)
{
return $this->fetch_results('/api/s/' . $this->site . '/stat/sta/' . strtolower(trim($client_mac)));
if (is_string($client_mac)) {
$client_mac = strtolower(trim($client_mac));
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/sta/' . $client_mac);
}
/**
* Fetch details for a single client device
*
* @param string $client_mac optional, client device MAC address
* @param string $client_mac client device MAC address
* @return array returns an object with the client device information
*/
public function stat_client($client_mac)
@@ -1378,7 +1380,11 @@ class Client
*/
public function list_devices($device_mac = null)
{
return $this->fetch_results('/api/s/' . $this->site . '/stat/device/' . strtolower(trim($device_mac)));
if (is_string($device_mac)) {
$device_mac = strtolower(trim($device_mac));
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/device/' . $device_mac);
}
/**
@@ -1866,7 +1872,7 @@ class Client
public function create_hotspotop($name, $x_password, $note = null)
{
$payload = ['name' => $name, 'x_password' => $x_password];
if (!isset($note)) {
if (is_string($note)) {
$payload['note'] = trim($note);
}
@@ -1914,7 +1920,7 @@ class Client
'quota' => intval($quota),
];
if (!is_null($note)) {
if (is_string($note)) {
$payload['note'] = trim($note);
}
@@ -2649,7 +2655,7 @@ class Client
$payload = [];
$payload['x_passphrase'] = trim($x_passphrase);
if (!empty($name)) {
if (is_string($name)) {
$payload['name'] = trim($name);
}
@@ -3172,7 +3178,7 @@ class Client
****************************************************************/
/**
* Fetch access points and other devices under management of the controller (USW and/or USG devices)
* Fetch access points and other devices under management of the controller (USW, USG, and/or UnIfi OS consoles)
*
* NOTE:
* changed function/method name to fit its purpose