unifi-php-api/examples/auth_guest_with_note.php
malle-pietje d66c3ddd75 API client class v1.1.81
- 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
2023-12-18 11:11:27 +01:00

82 lines
2.4 KiB
PHP
Executable File

<?php
/**
* PHP API usage example
*
* contributed by: Art of WiFi
* description: example basic PHP script to auth a guest device and attach a note to it,
* this requires the device to be connected to the WLAN/LAN at moment of
* authorization
*/
/**
* 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 MAC address of the device to authorize
*/
$mac = '<enter MAC address of guest device to auth>';
/**
* the duration to authorize the device for in minutes
*/
$duration = 2000;
/**
* The site to authorize the device with
*/
$site_id = '<enter your site id here>';
/**
* the note to attach to the device
*/
$note = 'Note to attach to newly authorized device';
/**
* 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();
/**
* we authorize the device for the requested duration and attach the note to it's object
*/
$auth_result = $unifi_connection->authorize_guest($mac, $duration);
$getid_result = $unifi_connection->stat_client($mac);
$user_id = $getid_result[0]->_id;
$note_result = $unifi_connection->set_sta_note($user_id, $note);
/**
* When using older Controller versions (< 5.5.x) to attach a note to a new (unconnected) device, we instead need to take the
* following steps before authorizing the device:
* - first block the device to get an entry in the user collection
* - get the device id from the user collection
* - attach note to the device
* - then unblock the device again **after the authorization has taken place**
*/
//$block_result = $unifi_connection->block_sta($mac);
//$getid_result = $unifi_connection->stat_client($mac);
//$user_id = $getid_result[0]->_id;
//$note_result = $unifi_connection->set_sta_note($user_id, $note);
//$unblock_result = $unifi_connection->unblock_sta($mac);
//$auth_result = $unifi_connection->authorize_guest($mac, $duration);
/**
* provide feedback in json format
*/
echo json_encode($auth_result, JSON_PRETTY_PRINT);