Initial release of the Unifi API Client

This commit is contained in:
Joris van de Sande
2015-08-09 21:06:47 +02:00
commit 8ce264b2a0
11 changed files with 494 additions and 0 deletions

28
examples/README.md Normal file
View File

@@ -0,0 +1,28 @@
Examples
========
To run the examples, you must copy the config.example.php file to config.php
and change the configuration to your needs.
statistics.php
--------------
Fetches the client statistics for a given site.
php statistics.php
authorize-guest.php
-------------------
Authorizes a guest (mac address) for x minutes.
You need to login with a user that has full access to the Unifi controller.
php authorize-guest.php
unauthorize-guest.php
---------------------
Unauthorize a guest (mac address).
You need to login with a user that has full access to the Unifi controller.
php unauthorize-guest.php

View File

@@ -0,0 +1,35 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
use JVDS\UnifiApiClient\Client;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
$apiClient = new Client(new HttpClient(['base_uri' => $config['base_uri']]));
try {
// login to the unifi controller API
$apiClient->login($config['username'], $config['password']);
// Authorize guest with mac address 01:01:01:01:01:01 for 60 minutes
// You need a user with full access to the unifi controller for this call!
$responseBody = $apiClient->authorizeGuest($config['site'], '01:01:01:01:01:01', 60);
print_r(json_decode($responseBody));
$apiClient->logout();
} catch (RequestException $e) {
echo $e->getMessage() . PHP_EOL;
echo '----- Request ------' . PHP_EOL;
echo $e->getRequest()->getBody()->getContents();
echo PHP_EOL;
echo '----- Response ------' . PHP_EOL;
echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -';
echo PHP_EOL;
}

View File

@@ -0,0 +1,11 @@
<?php
return [
// The base uri of your Unifi controller
'base_uri' => 'https://127.0.0.1:8443',
// Your username (You should create a user with read only access to the Unifi controller)
'username' => '',
// Your password
'password' => '',
// The site name to run the examples against
'site' => 'default'
];

View File

@@ -0,0 +1,37 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
use JVDS\UnifiApiClient\Client;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
$apiClient = new Client(new HttpClient(['base_uri' => $config['base_uri']]));
try {
// login to the unifi controller API
$apiClient->login($config['username'], $config['password']);
// Fetch device statistics for the given site
$responseBody = $apiClient
->deviceStatistics($config['site'])
->getBody()
->getContents();
print_r(json_decode($responseBody, true));
$apiClient->logout();
} catch (RequestException $e) {
echo $e->getMessage() . PHP_EOL;
echo '----- Request ------' . PHP_EOL;
echo $e->getRequest()->getBody()->getContents();
echo PHP_EOL;
echo '----- Response ------' . PHP_EOL;
echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -';
echo PHP_EOL;
}

37
examples/statistics.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
use JVDS\UnifiApiClient\Client;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
$apiClient = new Client(new HttpClient(['base_uri' => $config['base_uri']]));
try {
// login to the unifi controller API
$apiClient->login($config['username'], $config['password']);
// Fetch statistics for the given site
$responseBody = $apiClient
->statistics($config['site'])
->getBody()
->getContents();
print_r(json_decode($responseBody));
$apiClient->logout();
} catch (RequestException $e) {
echo $e->getMessage() . PHP_EOL;
echo '----- Request ------' . PHP_EOL;
echo $e->getRequest()->getBody()->getContents();
echo PHP_EOL;
echo '----- Response ------' . PHP_EOL;
echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -';
echo PHP_EOL;
}

View File

@@ -0,0 +1,35 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
use JVDS\UnifiApiClient\Client;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
$apiClient = new Client(new HttpClient(['base_uri' => $config['base_uri']]));
try {
// login to the unifi controller API
$apiClient->login($config['username'], $config['password']);
// Revoke authorization for guest with mac address 01:01:01:01:01:01
// You need a user with full access to the unifi controller for this call!
$responseBody = $apiClient->unauthorizeGuest($config['site'], '01:01:01:01:01:01');
print_r(json_decode($responseBody));
$apiClient->logout();
} catch (RequestException $e) {
echo $e->getMessage() . PHP_EOL;
echo '----- Request ------' . PHP_EOL;
echo $e->getRequest()->getBody()->getContents();
echo PHP_EOL;
echo '----- Response ------' . PHP_EOL;
echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -';
echo PHP_EOL;
}