Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1714e9587c | ||
|
|
cd92192cb8 | ||
|
|
7c6cd4eb85 | ||
|
|
30f4230cfd | ||
|
|
88263f6863 | ||
|
|
1017d1461e | ||
|
|
2c035878ed | ||
|
|
de407e6ac0 | ||
|
|
006702db0c | ||
|
|
6f1b75b89a | ||
|
|
287c67c39e | ||
|
|
871ab82608 | ||
|
|
4dddd08d8b |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,2 +1,9 @@
|
||||
# Ignore personal config file
|
||||
examples/config.php
|
||||
/examples/config.php
|
||||
|
||||
# ignore accidentally generated vendor directory and composer.lock file, just in case...
|
||||
/vendor/
|
||||
/composer.lock
|
||||
|
||||
# ignore XML files
|
||||
*.xml
|
||||
31
README.md
31
README.md
@@ -1,8 +1,8 @@
|
||||
## UniFi Controller API client class
|
||||
|
||||
A PHP class which provides access to Ubiquiti's **UniFi Controller API**, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.29 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
|
||||
A PHP class which provides access to Ubiquiti's **UniFi Controller API**, versions 4.X.X and 5.X.X of the UniFi Controller software are supported (version 5.8.24 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
|
||||
|
||||
This class can be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
|
||||
This class can be installed manually or using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
|
||||
|
||||
## Methods and functions supported
|
||||
|
||||
@@ -15,22 +15,28 @@ The class currently supports the following functions/methods to get/post/put/del
|
||||
- authorize_guest()
|
||||
- block_sta()
|
||||
- count_alarms()
|
||||
- create_firewallgroup()
|
||||
- create_hotspotop()
|
||||
- create_network()
|
||||
- create_radius_account()
|
||||
- create_site()
|
||||
- create_usergroup()
|
||||
- create_user()
|
||||
- create_voucher()
|
||||
- create_wlan()
|
||||
- delete_device()
|
||||
- delete_firewallgroup()
|
||||
- delete_network()
|
||||
- delete_radius_account()
|
||||
- delete_site()
|
||||
- delete_usergroup()
|
||||
- delete_wlan()
|
||||
- disable_ap()
|
||||
- edit_client_fixedip()
|
||||
- edit_firewallgroup()
|
||||
- edit_usergroup()
|
||||
- extend_guest_validity()
|
||||
- forget_sta() (supported on controller version 5.9.X and higher)
|
||||
- invite_admin()
|
||||
- revoke_admin()
|
||||
- led_override()
|
||||
@@ -38,6 +44,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
||||
- list_all_admins()
|
||||
- list_alarms()
|
||||
- list_aps() (deprecated but still available as alias)
|
||||
- list_backups()
|
||||
- list_clients()
|
||||
- list_country_codes()
|
||||
- list_current_channels()
|
||||
@@ -47,6 +54,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
||||
- list_dynamicdns()
|
||||
- list_events()
|
||||
- list_extension()
|
||||
- list_firewallgroups()
|
||||
- list_guests()
|
||||
- list_health()
|
||||
- list_hotspotop()
|
||||
@@ -102,15 +110,22 @@ The class currently supports the following functions/methods to get/post/put/del
|
||||
- stat_allusers()
|
||||
- stat_auths()
|
||||
- stat_client()
|
||||
- stat_5minutes_aps() (supported on controller version 5.5.* and higher)
|
||||
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
|
||||
- stat_hourly_aps()
|
||||
- stat_daily_aps()
|
||||
- stat_5minutes_site() (supported on controller version 5.5.* and higher)
|
||||
- stat_5minutes_gateway() (supported on controller version 5.7.X and higher)
|
||||
- stat_hourly_gateway() (supported on controller version 5.7.X and higher)
|
||||
- stat_daily_gateway() (supported on controller version 5.7.X and higher)
|
||||
- stat_5minutes_site() (supported on controller version 5.5.X and higher)
|
||||
- stat_hourly_site()
|
||||
- stat_daily_site()
|
||||
- stat_5minutes_user (supported on controller version 5.7.X and higher)
|
||||
- stat_hourly_user() (supported on controller version 5.7.X and higher)
|
||||
- stat_daily_user() (supported on controller version 5.7.X and higher)
|
||||
- stat_payment()
|
||||
- stat_sessions()
|
||||
- stat_sites()
|
||||
- stat_speedtest_results()
|
||||
- stat_sta_sessions_latest()
|
||||
- stat_status()
|
||||
- stat_sysinfo()
|
||||
@@ -137,7 +152,7 @@ Please refer to the source code for more details on the functions/methods and th
|
||||
|
||||
## 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 and with PHP 7.0.7 and cURL 7.37.0)
|
||||
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running
|
||||
|
||||
## Installation ##
|
||||
@@ -215,7 +230,7 @@ Please refer to the `examples/` directory for some more detailed examples which
|
||||
|
||||
#### IMPORTANT NOTES:
|
||||
|
||||
1. The last parameter (`true`) that is passed to the constructor, enables validation of the controller's SSL certificate which is otherwise **disabled** by default. It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi Controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.
|
||||
1. The last optional parameter that is passed to the constructor in the above example (`true`), enables validation of the controller's SSL certificate which is otherwise **disabled** by default. It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi Controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.
|
||||
|
||||
2. In the example above, `$site_id` is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi Controller:
|
||||
|
||||
@@ -225,7 +240,7 @@ Please refer to the `examples/` directory for some more detailed examples which
|
||||
|
||||
## 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/questions.
|
||||
There is still work to be done to add functionality and further improve the usability of this class, so all suggestions/comments are welcome. Please use the GitHub [issue list](https://github.com/Art-of-WiFi/UniFi-API-client/issues) 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 suggestions and questions.
|
||||
|
||||
## Contribute
|
||||
|
||||
@@ -236,7 +251,7 @@ If you would like to contribute code (improvements), please open an issue and in
|
||||
This class is 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://dl.ubnt.com/unifi/5.7.20/unifi_sh_api
|
||||
- and the API as published by Ubiquiti: https://dl.ubnt.com/unifi/5.8.24/unifi_sh_api
|
||||
|
||||
## Important Disclaimer
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
## API client class usage examples
|
||||
|
||||
This directory contains some PHP code examples which demonstrate usage of the PHP API client class and can be used as a good starting point for your own custom code.
|
||||
|
||||
### Usage
|
||||
@@ -6,6 +7,8 @@ This directory contains some PHP code examples which demonstrate usage of the PH
|
||||
Copy the appropriate example file to your working directory together with a copy of the config.template.php file which should be renamed to config.php.
|
||||
Then update the contents of your new config.php with your controller details and credentials and modify the example file as required to fit your needs.
|
||||
|
||||
Also make sure to update the path for the composer autoloader file (`vendor/autoload.php`) or the file containing the Class itself (`src/Client.php`) in your `require_once()` statement as required.
|
||||
|
||||
### Contribute
|
||||
|
||||
If you would like to share your own example file(s), please open an issue and include your code there or else create a pull request.
|
||||
|
||||
84
examples/block_list.php
Executable file
84
examples/block_list.php
Executable file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: @malcolmcif, based on another Art of WiFi example
|
||||
* description: basic PHP script to block a list of mac addresses passed in via command line,
|
||||
* output is to console in non json format
|
||||
*
|
||||
* usage:
|
||||
* php block_list.php <list of comma seperated mac addresses>
|
||||
*
|
||||
* example:
|
||||
* php block_list.php 09:09:09:09:09:09,10:10:10:10:10:10
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* using the composer autoloader
|
||||
*/
|
||||
require_once('vendor/autoload.php');
|
||||
|
||||
/**
|
||||
* include the config file (place your credentials etc. there if not already present)
|
||||
* see the config.template.php file for an example
|
||||
*/
|
||||
require_once('config.php');
|
||||
|
||||
$debug = false;
|
||||
/**
|
||||
* the MAC address(es) of the device(s) to block
|
||||
*/
|
||||
$macs_to_block = explode(',',$argv[1]);
|
||||
|
||||
/**
|
||||
* The site to authorize the device with
|
||||
*/
|
||||
$site_id = 'MUST_DEFINE_THIS';
|
||||
if ($site_id == "MUST_DEFINE_THIS") {
|
||||
print 'ERROR: set the site id in your script';
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the UniFi API connection class and log in to the controller
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
|
||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
||||
|
||||
foreach ($macs_to_block as &$mac) {
|
||||
// block_result is always true even if mac address does not exist :(
|
||||
$block_result = $unifi_connection->block_sta($mac);
|
||||
|
||||
/**
|
||||
* NOTE:
|
||||
* during testing I had some strange behavior where clients were not reconnecting to the network correctly,
|
||||
* they appeared unblocked and received a valid IP address but could not actually get any data.
|
||||
* the clients did not come to life until I disabled the SSID and then re enabled it.
|
||||
* I guessed maybe these commands were occurring too quickly for the controller so I have slowed them down;
|
||||
* since introducing the sleep I have not seen the above behavior so it might be fixed
|
||||
*/
|
||||
sleep(1);
|
||||
|
||||
$getid_result = $unifi_connection->stat_client($mac);
|
||||
|
||||
if (property_exists($getid_result[0], "oui")) {
|
||||
// this field(manufacturer) seems to exist on valid mac addresses
|
||||
if (property_exists($getid_result[0], "name")) {
|
||||
// this is the alias field if it has been defined
|
||||
$name = $getid_result[0]->name;
|
||||
} else {
|
||||
$name = $getid_result[0]->hostname;
|
||||
}
|
||||
print 'blocked ' . $name . PHP_EOL;
|
||||
} else {
|
||||
print 'ERROR: could not block ' . $mac . PHP_EOL;
|
||||
print ' check mac address is valid and part of your network' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* No json formatted data
|
||||
*/
|
||||
//echo json_encode($block_result, JSON_PRETTY_PRINT);
|
||||
@@ -29,20 +29,18 @@ if ($loginresults === 400) {
|
||||
print "UniFi controller login failure, please check your credentials in config.php.\n";
|
||||
} else {
|
||||
$guestlist = $unifi_connection->list_guests();
|
||||
// print "<pre>"; print_r ($guestlist); print "</pre>";
|
||||
// loop thru all known guests
|
||||
foreach ($guestlist as $guest) {
|
||||
// print "<pre>"; print_r ($guest); print "</pre>";
|
||||
print "<pre>" . $guest->_id . " (" . $guest->mac . "), valid until " . date (DATE_ATOM, $guest->end) . " (" . $guest->end . ")</pre>";
|
||||
print "<pre>" . $guest->_id . " (" . $guest->mac . "), valid until " . date(DATE_ATOM, $guest->end) . " (" . $guest->end . ")</pre>";
|
||||
|
||||
// just a sample: only extend validity of guests which have end date after 2017-04-02
|
||||
if ($guest->end > 1491166482) {
|
||||
// extend clients five times = five days
|
||||
if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
examples/list_user_stats.php
Executable file
51
examples/list_user_stats.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: Art of WiFi
|
||||
* description: example basic PHP script to pull stats for s epcific user/client device from the UniFi controller and output in json format
|
||||
*/
|
||||
|
||||
/**
|
||||
* using the composer autoloader
|
||||
*/
|
||||
require_once('vendor/autoload.php');
|
||||
|
||||
/**
|
||||
* include the config file (place your credentials etc. there if not already present)
|
||||
* see the config.template.php file for an example
|
||||
*/
|
||||
require_once('config.php');
|
||||
|
||||
/**
|
||||
* the site to use
|
||||
*/
|
||||
$site_id = '<enter your site id here>';
|
||||
|
||||
/**
|
||||
* MAC address of client to fetch stats for
|
||||
*/
|
||||
$mac = '<MAC address>';
|
||||
|
||||
/**
|
||||
* array of attributes to collect
|
||||
* valid attributes:
|
||||
* rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets, tx_packets
|
||||
*/
|
||||
//$attribs = ['rx_bytes', 'tx_bytes', 'signal', 'rx_rate', 'tx_rate', 'rx_retries', 'tx_retries', 'rx_packets', 'tx_packets'];
|
||||
$attribs = ['rx_bytes', 'tx_bytes'];
|
||||
|
||||
/**
|
||||
* initialize the UniFi API connection class and log in to the controller and do our thing
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, true);
|
||||
$set_debug_mode = $unifi_connection->set_debug(false);
|
||||
$loginresults = $unifi_connection->login();
|
||||
//$data = $unifi_connection->stat_5minutes_user($mac, null, null, $attribs);
|
||||
//$data = $unifi_connection->stat_hourly_user($mac, null, null, $attribs);
|
||||
$data = $unifi_connection->stat_daily_user($mac, null, null, $attribs);
|
||||
|
||||
/**
|
||||
* provide feedback in json format
|
||||
*/
|
||||
echo json_encode($data, JSON_PRETTY_PRINT);
|
||||
83
examples/unblock_list.php
Executable file
83
examples/unblock_list.php
Executable file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: @malcolmcif, based on another Art of WiFi example
|
||||
* description: basic PHP script to unblock a list of mac addresses passed in via command line,
|
||||
* output is to console in non json format
|
||||
*
|
||||
* usage:
|
||||
* php unblock_list.php <list of comma seperated mac addresses>
|
||||
*
|
||||
* example:
|
||||
* php unblock_list.php 09:09:09:09:09:09,10:10:10:10:10:10
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* using the composer autoloader
|
||||
*/
|
||||
require_once('vendor/autoload.php');
|
||||
|
||||
/**
|
||||
* include the config file (place your credentials etc. there if not already present)
|
||||
* see the config.template.php file for an example
|
||||
*/
|
||||
require_once('config.php');
|
||||
|
||||
$debug=false;
|
||||
/**
|
||||
* the MAC addresses of the device(s) to unblock
|
||||
*/
|
||||
$macs_to_unblock = explode(',', $argv[1]);
|
||||
|
||||
/**
|
||||
* The site to authorize the device with
|
||||
*/
|
||||
$site_id = 'MUST_DEFINE_THIS';
|
||||
if ($site_id == "MUST_DEFINE_THIS") {
|
||||
print 'ERROR: set the site id in your script';
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the UniFi API connection class and log in to the controller
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
|
||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
||||
|
||||
foreach ($macs_to_unblock as &$mac) {
|
||||
// block_result is always true even if mac address does not exist :(
|
||||
$block_result = $unifi_connection->unblock_sta($mac);
|
||||
|
||||
/**
|
||||
* NOTE:
|
||||
* during testing I had some strange behavior where clients were not reconnecting to the network correctly,
|
||||
* they appeared unblocked and received a valid IP address but could not actually get any data.
|
||||
* the clients did not come to life until I disabled the SSID and then re enabled it.
|
||||
* I guessed maybe these commands were occurring too quickly for the controller so I have slowed them down;
|
||||
* since introducing the sleep I have not seen the above behavior so it might be fixed
|
||||
*/
|
||||
sleep(1);
|
||||
|
||||
$getid_result = $unifi_connection->stat_client($mac);
|
||||
|
||||
if (property_exists($getid_result[0], "oui")) {
|
||||
// this field(manufacturer) seems to exist on valid mac addresses
|
||||
if (property_exists($getid_result[0], "name")) {
|
||||
$name = $getid_result[0]->name;
|
||||
} else {
|
||||
$name = $getid_result[0]->hostname;
|
||||
}
|
||||
print 'unblocked ' . $name . PHP_EOL;
|
||||
} else {
|
||||
print 'ERROR: could not unblock ' . $mac . PHP_EOL;
|
||||
print ' check mac address is valid and part of your network' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* provide feedback in json format
|
||||
*/
|
||||
//echo json_encode($block_result, JSON_PRETTY_PRINT);
|
||||
@@ -67,7 +67,7 @@ $data = $unifi_connection->list_devices($device_mac);
|
||||
$device_id = $data[0]->device_id;
|
||||
$update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config);
|
||||
|
||||
if(!$update_device){
|
||||
if (!$update_device) {
|
||||
$error = $unifi_connection->get_last_results_raw();
|
||||
echo json_encode($error, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
@@ -58,13 +58,13 @@ $data = $unifi_connection->list_devices($ap_mac);
|
||||
$radio_table = $data[0]->radio_table;
|
||||
$device_id = $data[0]->device_id;
|
||||
|
||||
foreach($radio_table as $radio){
|
||||
if($radio->radio === 'ng'){
|
||||
foreach ($radio_table as $radio) {
|
||||
if ($radio->radio === 'ng') {
|
||||
$radio->tx_power_mode = $ng_tx_power_mode;
|
||||
$radio->channel = $ng_channel;
|
||||
}
|
||||
|
||||
if($radio->radio === 'na'){
|
||||
if ($radio->radio === 'na') {
|
||||
$radio->tx_power_mode = $na_tx_power_mode;
|
||||
$radio->channel = $na_channel;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ foreach($radio_table as $radio){
|
||||
|
||||
$update_device = $unifi_connection->set_device_settings_base($device_id, ['radio_table' => $radio_table]);
|
||||
|
||||
if(!$update_device){
|
||||
if (!$update_device) {
|
||||
$error = $unifi_connection->get_last_results_raw();
|
||||
echo json_encode($error, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
2133
src/Client.php
2133
src/Client.php
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user