Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1714e9587c | ||
|
|
cd92192cb8 | ||
|
|
7c6cd4eb85 | ||
|
|
30f4230cfd | ||
|
|
88263f6863 | ||
|
|
1017d1461e | ||
|
|
2c035878ed | ||
|
|
de407e6ac0 | ||
|
|
006702db0c | ||
|
|
6f1b75b89a | ||
|
|
287c67c39e |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,2 +1,9 @@
|
|||||||
# Ignore personal config file
|
# 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
|
||||||
16
README.md
16
README.md
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
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).
|
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
|
## Methods and functions supported
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- authorize_guest()
|
- authorize_guest()
|
||||||
- block_sta()
|
- block_sta()
|
||||||
- count_alarms()
|
- count_alarms()
|
||||||
|
- create_firewallgroup()
|
||||||
- create_hotspotop()
|
- create_hotspotop()
|
||||||
- create_network()
|
- create_network()
|
||||||
- create_radius_account()
|
- create_radius_account()
|
||||||
@@ -24,12 +25,15 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- create_voucher()
|
- create_voucher()
|
||||||
- create_wlan()
|
- create_wlan()
|
||||||
- delete_device()
|
- delete_device()
|
||||||
|
- delete_firewallgroup()
|
||||||
- delete_network()
|
- delete_network()
|
||||||
- delete_radius_account()
|
- delete_radius_account()
|
||||||
- delete_site()
|
- delete_site()
|
||||||
- delete_usergroup()
|
- delete_usergroup()
|
||||||
- delete_wlan()
|
- delete_wlan()
|
||||||
- disable_ap()
|
- disable_ap()
|
||||||
|
- edit_client_fixedip()
|
||||||
|
- edit_firewallgroup()
|
||||||
- edit_usergroup()
|
- edit_usergroup()
|
||||||
- extend_guest_validity()
|
- extend_guest_validity()
|
||||||
- forget_sta() (supported on controller version 5.9.X and higher)
|
- forget_sta() (supported on controller version 5.9.X and higher)
|
||||||
@@ -40,6 +44,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- list_all_admins()
|
- list_all_admins()
|
||||||
- list_alarms()
|
- list_alarms()
|
||||||
- list_aps() (deprecated but still available as alias)
|
- list_aps() (deprecated but still available as alias)
|
||||||
|
- list_backups()
|
||||||
- list_clients()
|
- list_clients()
|
||||||
- list_country_codes()
|
- list_country_codes()
|
||||||
- list_current_channels()
|
- list_current_channels()
|
||||||
@@ -49,6 +54,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- list_dynamicdns()
|
- list_dynamicdns()
|
||||||
- list_events()
|
- list_events()
|
||||||
- list_extension()
|
- list_extension()
|
||||||
|
- list_firewallgroups()
|
||||||
- list_guests()
|
- list_guests()
|
||||||
- list_health()
|
- list_health()
|
||||||
- list_hotspotop()
|
- list_hotspotop()
|
||||||
@@ -107,6 +113,9 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
|
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
|
||||||
- stat_hourly_aps()
|
- stat_hourly_aps()
|
||||||
- stat_daily_aps()
|
- stat_daily_aps()
|
||||||
|
- 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_5minutes_site() (supported on controller version 5.5.X and higher)
|
||||||
- stat_hourly_site()
|
- stat_hourly_site()
|
||||||
- stat_daily_site()
|
- stat_daily_site()
|
||||||
@@ -116,6 +125,7 @@ The class currently supports the following functions/methods to get/post/put/del
|
|||||||
- stat_payment()
|
- stat_payment()
|
||||||
- stat_sessions()
|
- stat_sessions()
|
||||||
- stat_sites()
|
- stat_sites()
|
||||||
|
- stat_speedtest_results()
|
||||||
- stat_sta_sessions_latest()
|
- stat_sta_sessions_latest()
|
||||||
- stat_status()
|
- stat_status()
|
||||||
- stat_sysinfo()
|
- stat_sysinfo()
|
||||||
@@ -220,7 +230,7 @@ Please refer to the `examples/` directory for some more detailed examples which
|
|||||||
|
|
||||||
#### IMPORTANT NOTES:
|
#### 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:
|
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:
|
||||||
|
|
||||||
@@ -230,7 +240,7 @@ Please refer to the `examples/` directory for some more detailed examples which
|
|||||||
|
|
||||||
## 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/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
|
## Contribute
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
## API client class usage examples
|
## 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.
|
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
|
### 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.
|
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.
|
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
|
### 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.
|
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.
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ require_once('vendor/autoload.php');
|
|||||||
*/
|
*/
|
||||||
require_once('config.php');
|
require_once('config.php');
|
||||||
|
|
||||||
$debug=false;
|
$debug = false;
|
||||||
/**
|
/**
|
||||||
* the MAC address(es) of the device(s) to block
|
* the MAC address(es) of the device(s) to block
|
||||||
*/
|
*/
|
||||||
$macs_to_block = explode(',',$argv[1]);
|
$macs_to_block = explode(',',$argv[1]);
|
||||||
|
|
||||||
@@ -35,8 +35,7 @@ $macs_to_block = explode(',',$argv[1]);
|
|||||||
* The site to authorize the device with
|
* The site to authorize the device with
|
||||||
*/
|
*/
|
||||||
$site_id = 'MUST_DEFINE_THIS';
|
$site_id = 'MUST_DEFINE_THIS';
|
||||||
if ($site_id == "MUST_DEFINE_THIS")
|
if ($site_id == "MUST_DEFINE_THIS") {
|
||||||
{
|
|
||||||
print 'ERROR: set the site id in your script';
|
print 'ERROR: set the site id in your script';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -48,8 +47,7 @@ $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $
|
|||||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||||
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
||||||
|
|
||||||
foreach ($macs_to_block as &$mac)
|
foreach ($macs_to_block as &$mac) {
|
||||||
{
|
|
||||||
// block_result is always true even if mac address does not exist :(
|
// block_result is always true even if mac address does not exist :(
|
||||||
$block_result = $unifi_connection->block_sta($mac);
|
$block_result = $unifi_connection->block_sta($mac);
|
||||||
|
|
||||||
@@ -65,20 +63,16 @@ foreach ($macs_to_block as &$mac)
|
|||||||
|
|
||||||
$getid_result = $unifi_connection->stat_client($mac);
|
$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], "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
|
if (property_exists($getid_result[0], "name")) {
|
||||||
{
|
// this is the alias field if it has been defined
|
||||||
$name = $getid_result[0]->name;
|
$name = $getid_result[0]->name;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$name = $getid_result[0]->hostname;
|
$name = $getid_result[0]->hostname;
|
||||||
}
|
}
|
||||||
print 'blocked ' . $name . PHP_EOL;
|
print 'blocked ' . $name . PHP_EOL;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
print 'ERROR: could not block ' . $mac . PHP_EOL;
|
print 'ERROR: could not block ' . $mac . PHP_EOL;
|
||||||
print ' check mac address is valid and part of your network' . PHP_EOL;
|
print ' check mac address is valid and part of your network' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,20 +29,18 @@ if ($loginresults === 400) {
|
|||||||
print "UniFi controller login failure, please check your credentials in config.php.\n";
|
print "UniFi controller login failure, please check your credentials in config.php.\n";
|
||||||
} else {
|
} else {
|
||||||
$guestlist = $unifi_connection->list_guests();
|
$guestlist = $unifi_connection->list_guests();
|
||||||
// print "<pre>"; print_r ($guestlist); print "</pre>";
|
|
||||||
// loop thru all known guests
|
// loop thru all known guests
|
||||||
foreach ($guestlist as $guest) {
|
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
|
// just a sample: only extend validity of guests which have end date after 2017-04-02
|
||||||
if ($guest->end > 1491166482) {
|
if ($guest->end > 1491166482) {
|
||||||
// extend clients five times = five days
|
// 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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,14 +29,13 @@ $debug=false;
|
|||||||
/**
|
/**
|
||||||
* the MAC addresses of the device(s) to unblock
|
* the MAC addresses of the device(s) to unblock
|
||||||
*/
|
*/
|
||||||
$macs_to_unblock = explode(',',$argv[1]);
|
$macs_to_unblock = explode(',', $argv[1]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The site to authorize the device with
|
* The site to authorize the device with
|
||||||
*/
|
*/
|
||||||
$site_id = 'MUST_DEFINE_THIS';
|
$site_id = 'MUST_DEFINE_THIS';
|
||||||
if ($site_id == "MUST_DEFINE_THIS")
|
if ($site_id == "MUST_DEFINE_THIS") {
|
||||||
{
|
|
||||||
print 'ERROR: set the site id in your script';
|
print 'ERROR: set the site id in your script';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -48,10 +47,9 @@ $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $
|
|||||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||||
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
$loginresults = $unifi_connection->login(); // always true regardless of site id
|
||||||
|
|
||||||
foreach ($macs_to_unblock as &$mac)
|
foreach ($macs_to_unblock as &$mac) {
|
||||||
{
|
|
||||||
// block_result is always true even if mac address does not exist :(
|
// block_result is always true even if mac address does not exist :(
|
||||||
$block_result = $unifi_connection->unblock_sta($mac);
|
$block_result = $unifi_connection->unblock_sta($mac);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE:
|
* NOTE:
|
||||||
@@ -63,22 +61,17 @@ foreach ($macs_to_unblock as &$mac)
|
|||||||
*/
|
*/
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
$getid_result = $unifi_connection->stat_client($mac);
|
$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], "oui")) {
|
||||||
{
|
// this field(manufacturer) seems to exist on valid mac addresses
|
||||||
if (property_exists($getid_result[0], "name"))
|
if (property_exists($getid_result[0], "name")) {
|
||||||
{
|
|
||||||
$name = $getid_result[0]->name;
|
$name = $getid_result[0]->name;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$name = $getid_result[0]->hostname;
|
$name = $getid_result[0]->hostname;
|
||||||
}
|
}
|
||||||
print 'unblocked ' . $name . PHP_EOL;
|
print 'unblocked ' . $name . PHP_EOL;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
print 'ERROR: could not unblock ' . $mac . PHP_EOL;
|
print 'ERROR: could not unblock ' . $mac . PHP_EOL;
|
||||||
print ' check mac address is valid and part of your network' . PHP_EOL;
|
print ' check mac address is valid and part of your network' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ $data = $unifi_connection->list_devices($device_mac);
|
|||||||
$device_id = $data[0]->device_id;
|
$device_id = $data[0]->device_id;
|
||||||
$update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config);
|
$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();
|
$error = $unifi_connection->get_last_results_raw();
|
||||||
echo json_encode($error, JSON_PRETTY_PRINT);
|
echo json_encode($error, JSON_PRETTY_PRINT);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,13 +58,13 @@ $data = $unifi_connection->list_devices($ap_mac);
|
|||||||
$radio_table = $data[0]->radio_table;
|
$radio_table = $data[0]->radio_table;
|
||||||
$device_id = $data[0]->device_id;
|
$device_id = $data[0]->device_id;
|
||||||
|
|
||||||
foreach($radio_table as $radio){
|
foreach ($radio_table as $radio) {
|
||||||
if($radio->radio === 'ng'){
|
if ($radio->radio === 'ng') {
|
||||||
$radio->tx_power_mode = $ng_tx_power_mode;
|
$radio->tx_power_mode = $ng_tx_power_mode;
|
||||||
$radio->channel = $ng_channel;
|
$radio->channel = $ng_channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($radio->radio === 'na'){
|
if ($radio->radio === 'na') {
|
||||||
$radio->tx_power_mode = $na_tx_power_mode;
|
$radio->tx_power_mode = $na_tx_power_mode;
|
||||||
$radio->channel = $na_channel;
|
$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]);
|
$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();
|
$error = $unifi_connection->get_last_results_raw();
|
||||||
echo json_encode($error, JSON_PRETTY_PRINT);
|
echo json_encode($error, JSON_PRETTY_PRINT);
|
||||||
}
|
}
|
||||||
|
|||||||
2047
src/Client.php
2047
src/Client.php
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user