mirror of
https://github.com/Art-of-WiFi/UniFi-API-client.git
synced 2025-01-11 16:59:42 +01:00
minor changes
- more strict comparisons where possible - slightly changed error messages - enabled the CURLOPT_CONNECTTIMEOUT cURL option to timeout a controller connection after 10 seconds
This commit is contained in:
parent
8df2fcc522
commit
03cb083986
18
README.md
18
README.md
@ -6,7 +6,7 @@ This PHP class provides access to Ubiquiti's **UniFi Controller API**. Versions
|
|||||||
|
|
||||||
If you'd like to support further development of this PHP API client class, please use the PayPal donate button below. All donations go to the project maintainer.
|
If you'd like to support further development of this PHP API client class, please use the PayPal donate button below. All donations go to the project maintainer.
|
||||||
|
|
||||||
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
|
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
|
||||||
|
|
||||||
## Methods and functions supported
|
## Methods and functions supported
|
||||||
|
|
||||||
@ -113,13 +113,6 @@ Internal functions, getters/setters:
|
|||||||
|
|
||||||
Please refer to the source code for more details on each function/method and it's parameters.
|
Please refer to the source code for more details on each function/method and it's parameters.
|
||||||
|
|
||||||
## Credits
|
|
||||||
|
|
||||||
This class is largely based on the work done by the following developers:
|
|
||||||
- domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
|
|
||||||
- fbagnol: https://github.com/fbagnol/class.unifi.php
|
|
||||||
- and the API as published by Ubiquiti: https://www.ubnt.com/downloads/unifi/5.5.20/unifi_sh_api
|
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
|
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
|
||||||
@ -198,12 +191,19 @@ In this case, `jl3z2shm` is the value required for $site_id.
|
|||||||
|
|
||||||
## Need help or have suggestions?
|
## Need help or have suggestions?
|
||||||
|
|
||||||
There is still work to be done to add functionality and improve the usability of this class, so all suggestions/comments are welcome. Please use the github [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues) list or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/PHP-class-to-access-the-UniFi-controller-API-updates-and/td-p/1512870) to share your ideas.
|
There is still work to be done to add functionality and improve the usability of this class, so all suggestions/comments are welcome. Please use the github [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues) list or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/PHP-class-to-access-the-UniFi-controller-API-updates-and/td-p/1512870) to share your ideas/questions.
|
||||||
|
|
||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
If you would like to contribute code (improvements), please open an issue and include your code there or else create a pull request.
|
If you would like to contribute code (improvements), please open an issue and include your code there or else create a pull request.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
This class is largely based on the work done by the following developers:
|
||||||
|
- domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
|
||||||
|
- fbagnol: https://github.com/fbagnol/class.unifi.php
|
||||||
|
- and the API as published by Ubiquiti: https://www.ubnt.com/downloads/unifi/5.5.20/unifi_sh_api
|
||||||
|
|
||||||
## Important Disclaimer
|
## Important Disclaimer
|
||||||
|
|
||||||
Many of these functions are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi controller API.
|
Many of these functions are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi controller API.
|
||||||
|
@ -24,17 +24,16 @@ class Client
|
|||||||
/**
|
/**
|
||||||
* private properties
|
* private properties
|
||||||
*/
|
*/
|
||||||
private $user;
|
private $baseurl = 'https://127.0.0.1:8443';
|
||||||
private $password;
|
private $site = 'default';
|
||||||
private $baseurl = 'https://127.0.0.1:8443';
|
private $version = '5.4.16';
|
||||||
private $site = 'default';
|
private $debug = false;
|
||||||
private $version = '5.4.16';
|
private $is_loggedin = false;
|
||||||
private $debug = false;
|
private $cookies = '';
|
||||||
private $is_loggedin = false;
|
private $request_type = 'POST';
|
||||||
private $cookies = '';
|
private $connect_timeout = 10;
|
||||||
private $request_type = 'POST';
|
private $last_results_raw = null;
|
||||||
private $last_results_raw;
|
private $last_error_message = null;
|
||||||
private $last_error_message;
|
|
||||||
|
|
||||||
function __construct($user, $password, $baseurl = '', $site = '', $version = '')
|
function __construct($user, $password, $baseurl = '', $site = '', $version = '')
|
||||||
{
|
{
|
||||||
@ -60,7 +59,7 @@ class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strlen($this->site) !== 8 && $this->site !== 'default' && $this->debug) {
|
if (strlen($this->site) !== 8 && $this->site !== 'default' && $this->debug) {
|
||||||
error_log('The provided short site name is probably incorrect');
|
error_log('The provided (short) site name is probably incorrect');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +96,12 @@ class Client
|
|||||||
curl_setopt($ch, CURLOPT_URL, $this->baseurl.'/api/login');
|
curl_setopt($ch, CURLOPT_URL, $this->baseurl.'/api/login');
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
|
||||||
|
|
||||||
if (($content = curl_exec($ch)) === false) {
|
/**
|
||||||
|
* execute the cURL request
|
||||||
|
*/
|
||||||
|
$content = curl_exec($ch);
|
||||||
|
|
||||||
|
if (curl_errno($ch)) {
|
||||||
trigger_error('cURL error: '.curl_error($ch));
|
trigger_error('cURL error: '.curl_error($ch));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +163,6 @@ class Client
|
|||||||
* Set site
|
* Set site
|
||||||
* --------
|
* --------
|
||||||
* modify the private property site, returns the new (short) site name
|
* modify the private property site, returns the new (short) site name
|
||||||
* returns the new short name, or false if string length is incorrect or not 'default'
|
|
||||||
* required parameter <site> = string; must be the short site name of a site to which the
|
* required parameter <site> = string; must be the short site name of a site to which the
|
||||||
* provided credentials have access
|
* provided credentials have access
|
||||||
*
|
*
|
||||||
@ -168,13 +171,14 @@ class Client
|
|||||||
*/
|
*/
|
||||||
public function set_site($site)
|
public function set_site($site)
|
||||||
{
|
{
|
||||||
if (strlen($site) === 8 || $site === 'default') {
|
|
||||||
$this->site = $site;
|
|
||||||
|
|
||||||
return $this->site;
|
if (strlen($site) === 8 || $site === 'default') {
|
||||||
} else {
|
error_log('The provided (short) site name is probably incorrect');
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->site = $site;
|
||||||
|
|
||||||
|
return $this->site;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,15 +213,13 @@ class Client
|
|||||||
/**
|
/**
|
||||||
* Get last raw results
|
* Get last raw results
|
||||||
* --------------------
|
* --------------------
|
||||||
* returns the raw results of the last method called in PHP stdClass Object format by default, returns false if not set
|
* returns the raw results of the last method called, returns false if unavailable
|
||||||
* optional parameter <return_json> = boolean; true will return the results in "pretty printed" json format
|
* optional parameter <return_json> = boolean; true will return the results in "pretty printed" json format,
|
||||||
*
|
* PHP stdClass Object format is returned by default
|
||||||
* NOTE:
|
|
||||||
* this method can be used to get the full error as returned by the controller
|
|
||||||
*/
|
*/
|
||||||
public function get_last_results_raw($return_json = false)
|
public function get_last_results_raw($return_json = false)
|
||||||
{
|
{
|
||||||
if ($this->last_results_raw != null) {
|
if ($this->last_results_raw !== null) {
|
||||||
if ($return_json) {
|
if ($return_json) {
|
||||||
return json_encode($this->last_results_raw, JSON_PRETTY_PRINT);
|
return json_encode($this->last_results_raw, JSON_PRETTY_PRINT);
|
||||||
}
|
}
|
||||||
@ -231,11 +233,11 @@ class Client
|
|||||||
/**
|
/**
|
||||||
* Get last error message
|
* Get last error message
|
||||||
* ----------------------
|
* ----------------------
|
||||||
* returns the error message of the last method called in PHP stdClass Object format, returns false if not set
|
* returns the error message of the last method called in PHP stdClass Object format, returns false if unavailable
|
||||||
*/
|
*/
|
||||||
public function get_last_error_message()
|
public function get_last_error_message()
|
||||||
{
|
{
|
||||||
if (isset($this->last_error_message)) {
|
if ($this->last_error_message !== null) {
|
||||||
return $this->last_error_message;
|
return $this->last_error_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1884,7 +1886,12 @@ class Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($content = curl_exec($ch)) === false) {
|
/**
|
||||||
|
* execute the cURL request
|
||||||
|
*/
|
||||||
|
$content = curl_exec($ch);
|
||||||
|
|
||||||
|
if (curl_errno($ch)) {
|
||||||
trigger_error('cURL error: '.curl_error($ch));
|
trigger_error('cURL error: '.curl_error($ch));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1892,9 +1899,10 @@ class Client
|
|||||||
* has the session timed out?
|
* has the session timed out?
|
||||||
*/
|
*/
|
||||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$strerr = '{ "data" : [ ] , "meta" : { "msg" : "api.err.LoginRequired" , "rc" : "error"}}';
|
|
||||||
|
|
||||||
if ($httpcode == 401 && strcmp($content, $strerr) == 0) {
|
$json_decoded_content = json_decode($content, true);
|
||||||
|
|
||||||
|
if ($httpcode == 401 && isset($json_decoded_content['meta']['msg']) && $json_decoded_content['meta']['msg'] === 'api.err.LoginRequired') {
|
||||||
if ($this->debug) {
|
if ($this->debug) {
|
||||||
error_log('cURL debug: Needed reconnect to UniFi Controller');
|
error_log('cURL debug: Needed reconnect to UniFi Controller');
|
||||||
}
|
}
|
||||||
@ -1960,6 +1968,7 @@ class Client
|
|||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
|
||||||
|
|
||||||
if ($this->debug) {
|
if ($this->debug) {
|
||||||
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
||||||
|
Loading…
Reference in New Issue
Block a user