Skip to content

Commit

Permalink
API client class v1.1.81
Browse files Browse the repository at this point in the history
- hotfix to address cookie issues in UniFi OS 3.2.7, reported by @tflatebo
- fixed minor typos
- minor code reformatting of the examples
- starting with this release, cookies are no longer supported when connecting to a UniFi OS-based controller
- added set_vlan_to_port.php example, contributed by @SamuelSchnelly, #203
- allow additional parameters in `create_wlan()`'s payload, contributed by @sgrodzicki, #191
  • Loading branch information
malle-pietje committed Dec 18, 2023
1 parent 9e092f8 commit d66c3dd
Show file tree
Hide file tree
Showing 31 changed files with 508 additions and 148 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ The class automatically detects UniFi OS-based controllers and adjusts URLs and
If your own code implements strict validation of the URL that is passed to the constructor, please adapt your
logic to allow URLs without a port suffix or with port 443 when working with a UniFi OS-based controller.

> **IMPORTANT NOTE**: cookies are no longer supported with UniFi OS-based controllers. If your application code does use cookies,
they will be ignored automatically when working with UniFi OS-based controllers.

Please test all methods you plan on using thoroughly before using the API Client with
UniFi OS devices in a production environment.

Expand Down
9 changes: 8 additions & 1 deletion examples/ap_scanning_state.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
* initialize the UniFi API connection class and log in to the controller and do our thing
* spectrum_scan_state()
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$data = $unifi_connection->spectrum_scan_state($ap_mac);
Expand Down
11 changes: 9 additions & 2 deletions examples/ap_upgrade_firmware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
* initialize the UniFi API connection class, log in to the controller
* (this example assumes you have already assigned the correct values in config.php to the variables used)
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, false);
$login = $unifi_connection->login();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$login = $unifi_connection->login();

/**
* Run the actual upgrade
Expand Down
13 changes: 10 additions & 3 deletions examples/auth_guest_basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@
/**
* 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();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

/**
* then we authorize the device for the requested duration
Expand Down
13 changes: 10 additions & 3 deletions examples/auth_guest_with_note.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@
/**
* 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();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

/**
* we authorize the device for the requested duration and attach the note to it's object
Expand Down
13 changes: 10 additions & 3 deletions examples/block_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@
/**
* 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
$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 :(
Expand Down
21 changes: 14 additions & 7 deletions examples/change_super_mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,26 @@
/**
* 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($debug);
$loginresults = $unifi_connection->login();
$site_settings = $unifi_connection->list_settings();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$super_mgmt_settings = [];
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$site_settings = $unifi_connection->list_settings();

$super_mgmt_settings = [];
$super_mgmt_settings_id = '';

if (!empty($site_settings)) {
foreach($site_settings as $section) {
foreach ($site_settings as $section) {
echo 'section key: ' . $section->key . PHP_EOL;
if ($section->key === 'super_mgmt') {
$super_mgmt_settings = $section;
$super_mgmt_settings = $section;
$super_mgmt_settings_id = $section->_id;
}
}
Expand Down
15 changes: 11 additions & 4 deletions examples/change_wlan_password.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@
/**
* 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();
$results = $unifi_connection->set_wlansettings($wlan_id, $new_password);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$results = $unifi_connection->set_wlansettings($wlan_id, $new_password);

/**
* provide feedback in json format
Expand Down
13 changes: 10 additions & 3 deletions examples/create_site.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
/**
* 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);
$loginresults = $unifi_connection->login();
$results = $unifi_connection->create_site($description);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$loginresults = $unifi_connection->login();
$results = $unifi_connection->create_site($description);

/**
* provide feedback in json format
Expand Down
13 changes: 10 additions & 3 deletions examples/create_voucher.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@
/**
* 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();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

/**
* then we create the required number of vouchers with the requested expiration value
Expand Down
13 changes: 10 additions & 3 deletions examples/delete_site.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@
/**
* 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);
$loginresults = $unifi_connection->login();
$results = $unifi_connection->delete_site($site_to_delete);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$loginresults = $unifi_connection->login();
$results = $unifi_connection->delete_site($site_to_delete);

/**
* provide feedback in json format
Expand Down
13 changes: 10 additions & 3 deletions examples/disable_device.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
/**
* 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();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

/**
* then we disable the device
Expand Down
9 changes: 8 additions & 1 deletion examples/disable_switch_port.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@
/**
* 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, false);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$data = $unifi_connection->list_devices($device_mac);
Expand Down
15 changes: 11 additions & 4 deletions examples/execute_custom_api_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@
/**
* 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);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$results = $unifi_connection->custom_api_request($url, $request_method, $payload, $return);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$results = $unifi_connection->custom_api_request($url, $request_method, $payload, $return);

/**
* provide feedback in JSON format or as PHP Object
Expand Down
16 changes: 12 additions & 4 deletions examples/extend_guest_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
$site_id = "default";
$site_name = "*enter your site name*";

$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

if ($loginresults === 400) {
print "UniFi controller login failure, please check your credentials in config.php.\n";
Expand All @@ -36,7 +43,8 @@
* loop thru all known guests
*/
foreach ($guestlist as $guest) {
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
Expand Down
9 changes: 8 additions & 1 deletion examples/list_alarms.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
/**
* 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);
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$data = $unifi_connection->list_alarms();
Expand Down
15 changes: 11 additions & 4 deletions examples/list_ap_connected_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@
/**
* initialize the UniFi API connection class and log in to the controller and pull the requested data
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$aps_array = $unifi_connection->list_aps();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$aps_array = $unifi_connection->list_aps();

/**
* output the results in HTML format
Expand Down
15 changes: 11 additions & 4 deletions examples/list_connected_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
/**
* initialize the UniFi API connection class and log in to the controller and pull the requested data
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$clients_array = $unifi_connection->list_clients();
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$clients_array = $unifi_connection->list_clients();

/**
* output the results in JSON format
Expand Down
16 changes: 11 additions & 5 deletions examples/list_site_health.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@
/**
* initialize the UniFi API connection class and log in to the controller and pull the requested data
*/
$unifi_connection = new UniFi_API\Client(
$controlleruser,
$controllerpassword,
$controllerurl,
$site_id,
$controllerversion
);

$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->list_health();
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
$result = $unifi_connection->list_health();

/**
* output the results in correct json formatting
*/
header('Content-Type: application/json');
echo (json_encode($result, JSON_PRETTY_PRINT));
echo(json_encode($result, JSON_PRETTY_PRINT));
Loading

0 comments on commit d66c3dd

Please sign in to comment.