Skip to content

Commit

Permalink
minor code cleanup and various changes:
Browse files Browse the repository at this point in the history
- added a 6th parameter to the constructor to enable SSL cert verification, recommended for production environments
- added examples/change_wlan_password.php to demonstrate WLAN password/PSK change
- updated main README accordingly
  • Loading branch information
malle-pietje committed Oct 6, 2017
1 parent 971c77a commit 008280e
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 99 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## UniFi controller API client class

This PHP class provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.5.20 has been confirmed to work) are supported. It is an independent version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
This PHP class 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.5.20 has been confirmed to work). It is an independent version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).

The class is now also installable through composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.

### Donations

Expand Down Expand Up @@ -116,11 +118,11 @@ Please refer to the source code for more details on each function/method and it'
## Requirements

- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- network connectivity between this web server and the server and port (normally 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 ##

You can use **Composer**, **Git** or simply **Download the Release**
You can use **Composer**, **Git** or simply **Download the Release** to install the API client class.

### Composer

Expand Down Expand Up @@ -174,14 +176,19 @@ require_once('vendor/autoload.php');
* initialize the Unifi API connection class, log in to the controller and request the alarms collection
* (this example assumes you have already assigned the correct values to the variables used)
*/
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version);
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version, true);
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms(); // returns a PHP array containing alarm objects
```

Please refer to the `examples/` directory for some more detailed examples which you can use as a starting point for your own PHP code.

### NOTE:
### IMPORTANT NOTES:

In the example above, the last parameter (`true`, without quotes) that is passed to the constructor enables validation of the controller's SSL certificate which is *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.

---

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:

Expand All @@ -206,4 +213,4 @@ This class is largely based on the work done by the following developers:

## 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 the functions in this API client class are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi controller API.
45 changes: 45 additions & 0 deletions examples/change_wlan_password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* PHP API usage example
*
* contributed by: Art of WiFi
* description: example basic PHP script to change the WPA2 password/PSK of a WLAN, returns true on success
*/

/**
* 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 which the WLAN you want to modify belongs
*/
$site_id = '<enter your (short) site name here>';

/**
* the id of the WLAN you wish to modify
*/
$wlan_id = '<the value of _id for the WLAN you wish to change>';

/**
* the new WPA2 password/PSK to apply to the above WLAN
*/
$new_password = '<new password goes here>';

/**
* 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->set_wlansettings($wlan_id, $new_password);

/**
* provide feedback in json format
*/
echo json_encode($loginresults, JSON_PRETTY_PRINT);
19 changes: 12 additions & 7 deletions examples/create_voucher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHP API usage example
*
* contributed by: Art of WiFi
* description: example basic PHP script to create a set of vouchers
* description: example basic PHP script to create a set of vouchers, returns an array containing the newly created vouchers
*/

/**
Expand All @@ -18,9 +18,9 @@
require_once('config.php');

/**
* the voucher duration in minutes
* minutes the voucher is valid after activation (expiration time)
*/
$voucher_duration = 2000;
$voucher_expiration = 2000;

/**
* the number of vouchers to create
Expand All @@ -40,11 +40,16 @@
$loginresults = $unifi_connection->login();

/**
* then we create the required number of vouchers for the requested duration
* then we create the required number of vouchers with the requested expiration value
*/
$voucher_result = $unifi_connection->create_voucher($voucher_duration, $voucher_count);
$voucher_result = $unifi_connection->create_voucher($voucher_expiration, $voucher_count);

/**
* provide feedback (the newly created voucher code, without the dash) in json format
* we then fetch the newly created vouchers by the create_time returned
*/
echo json_encode($voucher_result, JSON_PRETTY_PRINT);
$vouchers = $unifi_connection->stat_voucher($voucher_result[0]->create_time);

/**
* provide feedback (the newly created vouchers) in json format
*/
echo json_encode($vouchers, JSON_PRETTY_PRINT);
5 changes: 3 additions & 2 deletions examples/test_connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

/**
* This cURL option can have a value of 0-6 see this URL for more details:
* This cURL option can have a value of 0-6
* see this URL for more details:
* http://php.net/manual/en/function.curl-setopt.php
* 0 is the default value and is used by the PHP API client class
*/
Expand Down Expand Up @@ -64,4 +65,4 @@

print PHP_EOL . '$results:' . PHP_EOL;
print_r($results);
print PHP_EOL;
print PHP_EOL;
Loading

0 comments on commit 008280e

Please sign in to comment.