-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-client-operations.php
More file actions
executable file
·148 lines (124 loc) · 6.18 KB
/
03-client-operations.php
File metadata and controls
executable file
·148 lines (124 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Example 3: Client Operations
*
* This example demonstrates how to work with connected clients,
* including listing clients, filtering, and performing client actions.
*/
require_once __DIR__.'/../vendor/autoload.php';
use ArtOfWiFi\UnifiNetworkApplicationApi\UnifiClient;
$config = require_once __DIR__.'/config.php';
// Configuration - Update the values in the config.php file
$controllerUrl = $config['base_url'];
$apiKey = $config['api_key'];
$siteId = $config['site_id'];
$verifySsl = $config['verify_ssl'];
try {
// Initialize the API client
$apiClient = new UnifiClient($controllerUrl, $apiKey, $verifySsl);
$apiClient->setSiteId($siteId);
echo "UniFi API Client - Client Operations Example\n";
echo str_repeat('=', 50)."\n\n"; // 1. List all connected clients
echo "1. Listing all connected clients...\n";
$clientsResponse = $apiClient->clients()->list();
$clients = $clientsResponse->json();
$totalClients = isset($clients['data']) ? count($clients['data']) : 0;
echo " Total connected clients: {$totalClients}\n\n";
if ($totalClients > 0) {
// Categorize clients
$wiredClients = [];
$wirelessClients = [];
$vpnClients = [];
$teleportClients = [];
foreach ($clients['data'] as $clientData) {
if ($clientData['type'] === 'WIRED') {
$wiredClients[] = $clientData;
} elseif ($clientData['type'] === 'WIRELESS') {
$wirelessClients[] = $clientData;
} elseif ($clientData['type'] === 'VPN') {
$vpnClients[] = $clientData;
} else {
$teleportClients[] = $clientData;
}
}
echo " Breakdown:\n";
echo ' - Wired clients: '.count($wiredClients)."\n";
echo ' - Wireless clients: '.count($wirelessClients)."\n";
echo ' - VPN clients: '.count($vpnClients)."\n";
echo ' - Teleport clients: '.count($teleportClients)."\n\n";
// 2. Display wireless client details
if (count($wirelessClients) > 0) {
echo "2. Wireless Client Details (first 5):\n";
$displayCount = min(5, count($wirelessClients));
for ($i = 0; $i < $displayCount; $i++) {
$client = $wirelessClients[$i];
$name = $client['name'] ?? $client['macAddress'] ?? 'Unknown';
$mac = $client['macAddress'] ?? 'Unknown';
$ip = $client['ipAddress'] ?? 'No IP';
$connectedAt = $client['connectedAt'] ?? 'Unknown';
echo " Client: {$name}\n";
echo " - MAC: {$mac}\n";
echo " - IP: {$ip}\n";
echo " - Connected at: {$connectedAt}\n";
echo "\n";
}
}
// 3. Get specific client details
if ($totalClients > 0) {
$firstClient = $clients['data'][0];
$clientId = $firstClient['id'];
echo "3. Getting detailed information for a specific client...\n";
echo " Client ID: {$clientId}\n";
$detailResponse = $apiClient->clients()->get($clientId);
try {
$details = $detailResponse->json();
} catch (Exception $e) {
echo ' Error: '.$e->getMessage()."\n";
}
echo ' Hostname: '.($details['name'] ?? 'Unknown')."\n";
echo ' IP: '.($details['ipAddress'] ?? 'No IP')."\n";
echo ' Connected at: '.($details['connectedAt'] ?? 'Unknown')."\n";
echo ' Access type: '.($details['access']['type'] ?? 'Unknown')."\n";
echo "\n";
}
}// 4. Filtering examples
echo "4. Filtering Examples (based on official API filterable properties):\n";
echo " Available filterable properties:\n";
echo " - id (UUID), type (STRING), macAddress (STRING), ipAddress (STRING)\n";
echo " - connectedAt (TIMESTAMP), access.type (STRING), access.authorized (BOOLEAN)\n\n";
echo " Get only wireless clients:\n";
echo " \$apiClient->clients()->list(filter: 'type.eq(\"WIRELESS\")');\n\n";
echo " Get only wired clients:\n";
echo " \$apiClient->clients()->list(filter: 'type.eq(\"WIRED\")');\n\n";
echo " Get only guest clients:\n";
echo " \$apiClient->clients()->list(filter: 'access.type.eq(\"GUEST\")');\n\n";
echo " Get clients by IP address:\n";
echo " \$apiClient->clients()->list(filter: 'ipAddress.eq(\"192.168.1.100\")');\n\n";
echo " Get recently connected clients (last hour):\n";
$timestamp = (new DateTime('-1 hour'))->format('Y-m-d\\TH:i:s\\Z');
echo " \$apiClient->clients()->list(filter: \"connectedAt.gt(\\$timestamp)\");\n\n"; // Example: Get only wireless clients
$wirelessResponse = $apiClient->clients()->list(filter: 'type.eq("WIRELESS")');
$wirelessData = $wirelessResponse->json();
$wirelessCount = isset($wirelessData['data']) ? count($wirelessData['data']) : 0;
echo " Filtered wireless clients: {$wirelessCount}\n\n"; // 5. Client actions
echo "5. Client Actions (examples - commented out for safety)\n";
echo " According to the official API specification, the only documented actions are:\n";
echo " - Authorize a guest client:\n";
echo " \$apiClient->clients()->executeAction(\$clientId, [\n";
echo " 'action' => 'AUTHORIZE_GUEST_ACCESS',\n";
echo " 'timeLimitMinutes' => 480, // Optional: 8 hours (range: 1-1000000)\n";
echo " 'dataUsageLimitMBytes' => 1024 // Optional: 1GB data limit (range: 1-1048576)\n";
echo " 'rxRateLimitKbps' => 100000 // Optional: download rate limit in kilobits per second (range: 2-100000)\n";
echo " 'txRateLimitKbps' => 100000 // Optional: upload rate limit in kilobits per second (range: 2-100000)\n";
echo " ]);\n";
echo "\n";
echo " - Unauthorize a guest client:\n";
echo " \$apiClient->clients()->executeAction(\$clientId, [\n";
echo " 'action' => 'UNAUTHORIZE_GUEST_ACCESS'\n";
echo " ]);\n";
echo "\n";
echo str_repeat('=', 50)."\n";
echo "Example completed successfully!\n";
} catch (Exception $e) {
echo 'ERROR: '.$e->getMessage()."\n";
}