Compare commits

...

13 Commits

Author SHA1 Message Date
malle-pietje
1714e9587c general code cleanup
added spaces throughout Client.php following PSR-2 guidelines that were skipped with the previous commit
added visibility to constructor and destructor methods following PSR-2 recommendations
declare user and password properties and visibility following PSR-2 recommendations
updated README to add recently added functions/methods
minor general changes to the README
2018-10-20 14:29:08 +02:00
malle-pietje
cd92192cb8 API client class v1.1.34
- added spaces throughout Client.php following PSR-2 guidelines
- changed checks to prevent 0 (zero) values from being submitted to the API for routes where this can have negative side effects
2018-10-10 10:14:00 +02:00
malle-pietje
7c6cd4eb85 removed and added spaces, and changed brackets usage following PSR-2 guidelines 2018-10-02 13:16:48 +02:00
malle-pietje
30f4230cfd Merge branch 'master' of github.com:Art-of-WiFi/UniFi-API-client into tempbranch 2018-10-02 12:10:46 +02:00
Jamie Aitken
88263f6863 getters and setters for private variables (#28)
Merging PR #28
2018-10-02 11:12:34 +02:00
malle-pietje
1017d1461e Commiting some changes to tempbranch before merging in a new PR 2018-10-02 11:10:46 +02:00
malle-pietje
2c035878ed - added stat_speedtest_results() method/function to collect results from scheduled speed tests
- added list_backups() method/function to list auto backups
2018-08-28 14:44:49 +02:00
malle-pietje
de407e6ac0 cleanup of merge conflicts 2018-08-19 12:13:53 +02:00
malle-pietje
006702db0c Merge branch 'master' of github.com:Art-of-WiFi/UniFi-API-client
resilve merge conflicts
2018-08-19 12:02:32 +02:00
malle-pietje
6f1b75b89a updated comments for stats-related methods/function to make clear start/end values must be provided in milliseconds
changed use of objects as payload input for json_encode, to associative arrays for consistency
added functions/methods for firewall groups: list_firewallgroups(), create_firewallgroup(), edit_firewallgroup(), delete_firewallgroup()
added checks for required boolean parameters
2018-08-19 12:00:19 +02:00
Alex Paredes Martinez
287c67c39e Dev edit client fixedip (#24)
* Added Fixed ip function

Sets the value of use_fixedip and fixed_ip fields of a specific client.

* Added network_id field

-Optional fields network_id and fixed_ip
-If use_fixedip is false, network_id and fixed_ip will be ignored
-If else, send the data if defined (if not, previous values)

* fix typo (forgot to includo json_encode)

* Get specific network data by its ID
2018-08-19 11:59:03 +02:00
malle-pietje
871ab82608 added user/client device stats methods/functions:
stat_5minutes_user()
stat_hourly_user()
stat_daily_user()
added example to demonstrate use of these new functions
2018-07-02 15:06:31 +02:00
malle-pietje
4dddd08d8b added unblock_list.php and block_list.php examples, contributed by @malcolmcif
added create_user() method/function
added forget_sta() method/function which is supported on controller version 5.9.* and higher
2018-05-21 13:06:50 +02:00
10 changed files with 1898 additions and 520 deletions

9
.gitignore vendored
View File

@@ -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

View File

@@ -1,8 +1,8 @@
## UniFi Controller API client class ## 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 ## Methods and functions supported
@@ -15,22 +15,28 @@ 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()
- create_site() - create_site()
- create_usergroup() - create_usergroup()
- create_user()
- 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)
- invite_admin() - invite_admin()
- revoke_admin() - revoke_admin()
- led_override() - led_override()
@@ -38,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()
@@ -47,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()
@@ -102,15 +110,22 @@ The class currently supports the following functions/methods to get/post/put/del
- stat_allusers() - stat_allusers()
- stat_auths() - stat_auths()
- stat_client() - 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_hourly_aps()
- stat_daily_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_hourly_site()
- stat_daily_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_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()
@@ -137,7 +152,7 @@ Please refer to the source code for more details on the functions/methods and th
## 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 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 - network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running
## Installation ## ## Installation ##
@@ -215,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:
@@ -225,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
@@ -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: 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 - 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 - 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 ## Important Disclaimer

View File

@@ -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.

84
examples/block_list.php Executable file
View 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);

View File

@@ -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";
} }
} }

51
examples/list_user_stats.php Executable file
View 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
View 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);

View File

@@ -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);
} }

View File

@@ -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);
} }

File diff suppressed because it is too large Load Diff