Skip to content

Commit

Permalink
API client class v1.1.73
Browse files Browse the repository at this point in the history
- minor spacing changes based on Scrutinizer feedback
- updated create_wlan() method/function to work with the new way of assigning a VLAN which now requires passing the _id value
of the VLAN, reported by @BeneReuthlinger
- merged #132, README update, contributed by @pauloboc
- merged #133, adds edit_client_name() method, contributed by @pauloboc
  • Loading branch information
malle-pietje committed Oct 23, 2021
1 parent 01eafb5 commit 310abc4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ own PHP code.

The class currently supports the following functions/methods to GET/POST/PUT/DELETE data
through the UniFi Controller API. Please refer to the comments in the source code for
more details on the functions/methods and their respective parameters.
more details on each of the functions/methods and their respective parameters.

- login()
- logout()
Expand Down
27 changes: 14 additions & 13 deletions examples/test_connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
if (is_resource($ch) || is_object($ch)) {
/**
* If we have a resource or object (for PHP > 8.0), we proceed and set the required cURL options
*/
curl_setopt($ch, CURLOPT_URL, $controllerurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

/**
* This cURL option can have a value of 0-6
*
* NOTES:
* The cURL option CURLOPT_SSLVERSION 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
*/
curl_setopt($ch, CURLOPT_SSLVERSION, 0);
$curl_options = [
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_URL => $controllerurl,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true,
CURLOPT_SSLVERSION => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
];

/**
* Be more verbose
*/
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt_array($ch, $curl_options);

/**
* $results contains the output as returned by the cURL request,
Expand Down
36 changes: 18 additions & 18 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Client
* private and protected properties
*
* NOTE:
* do not modify the values here, instead user the constructor or the getter and setter functions/methods
* do not modify the values here, instead use the constructor or the getter and setter functions/methods
*/
const CLASS_VERSION = '1.1.73';
protected $baseurl = 'https://127.0.0.1:8443';
Expand Down Expand Up @@ -60,7 +60,7 @@ class Client
* @param string $version optional, the version number of the controller
* @param bool $ssl_verify optional, whether to validate the controller's SSL certificate or not, a value of true
* is recommended for production environments to prevent potential MitM attacks, default
* value (false) disables validation of the controller certificate
* value (false) disables validation of the controller's SSL certificate
*/
public function __construct($user, $password, $baseurl = '', $site = '', $version = '', $ssl_verify = false)
{
Expand Down Expand Up @@ -1057,23 +1057,23 @@ public function stat_client($client_mac)
/**
* Assign client device to another group
*
* @param string $user_id id of the user device to be modified
* @param string $group_id id of the user group to assign user to
* @param string $client_id _id value of the client device to be modified
* @param string $group_id _id value of the user group to assign client device to
* @return bool returns true upon success
*/
public function set_usergroup($user_id, $group_id)
public function set_usergroup($client_id, $group_id)
{
$payload = ['usergroup_id' => $group_id];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload);
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($client_id), $payload);
}

/**
* Update client fixedip (using REST)
* Update client device fixed IP address (using REST)
*
* @param string $client_id _id value for the client
* @param bool $use_fixedip determines whether use_fixedip is true or false
* @param string $client_id _id value for the client device
* @param bool $use_fixedip determines whether to enable the fixed IP address or not
* @param string $network_id optional, _id value for the network where the ip belongs to
* @param string $fixed_ip optional, IP address, value of client's fixed_ip field
* @param string $fixed_ip optional, IP address, value of client device's fixed_ip field
* @return array|false returns an array containing a single object with attributes of the updated client on success
*/
public function edit_client_fixedip($client_id, $use_fixedip, $network_id = null, $fixed_ip = null)
Expand Down Expand Up @@ -1102,10 +1102,10 @@ public function edit_client_fixedip($client_id, $use_fixedip, $network_id = null
}

/**
* Update client name (using REST)
* Update client device name (using REST)
*
* @param string $client_id _id value for the client
* @param bool $name of the client
* @param string $client_id _id value for the client device
* @param string $name name of the client
* @return array|false returns an array containing a single object with attributes of the updated client on success
*/
public function edit_client_name($client_id, $name)
Expand All @@ -1116,8 +1116,8 @@ public function edit_client_name($client_id, $name)

$this->curl_method = 'PUT';
$payload = [
'_id' => $client_id,
'name' => $name,
'_id' => $client_id,
'name' => $name,
];

return $this->fetch_results('/api/s/' . $this->site . '/rest/user/' . trim($client_id), $payload);
Expand Down Expand Up @@ -1825,7 +1825,7 @@ public function list_device_name_mappings()
/**
* Fetch self
*
* @return array containing information about the logged in user
* @return array containing information about the logged-in user
*/
public function list_self()
{
Expand Down Expand Up @@ -3948,7 +3948,7 @@ protected function exec_curl($path, $payload = null)
}

/**
* fetch the HTTP response code
* get the HTTP response code
*/
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Expand Down Expand Up @@ -4027,7 +4027,7 @@ protected function exec_curl($path, $payload = null)
/**
* Create and return a new cURL handle
*
* @return object|bool|resource cURL handle (object or resource) upon success, false upon failure
* @return object|resource|bool cURL handle (object or resource) upon success, false upon failure
*/
protected function get_curl_handle()
{
Expand Down

0 comments on commit 310abc4

Please sign in to comment.