PHP code example of beziworld / laravel-unifi

1. Go to this page and download the library: Download beziworld/laravel-unifi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beziworld / laravel-unifi example snippets


'connections' => [
    'default' => [...],
    'hotel-krakow' => [
        'url' => 'https://10.20.0.1',
        'api_key' => env('UNIFI_KRAKOW_API_KEY'),
        'site' => 'default',
        'verify_ssl' => storage_path('certs/unifi-krakow-ca.pem'),
    ],
],

use Beziworld\Unifi\Facades\Unifi;

// One voucher for two devices, valid until check-out (e.g. from a PMS reservation)
$voucher = Unifi::connection('hotel-krakow')->vouchers()->forStay(
    name: 'Room 204 / Smith',
    checkout: $reservation->departure->setTime(11, 0),
    devices: 2,
);

$voucher->formattedCode(); // "48614-09510"

// Batches with limits
use Beziworld\Unifi\Data\VoucherRequest;

$vouchers = Unifi::vouchers()->create(new VoucherRequest(
    name: 'Conference day',
    timeLimitMinutes: 600,
    count: 50,
    authorizedGuestLimit: 1,
    dataUsageLimitMBytes: 2048,
    rxRateLimitKbps: 20000,
    txRateLimitKbps: 5000,
));

Unifi::vouchers()->findByCode('48614-09510');
Unifi::vouchers()->purgeExpired();                           // returns the number of deleted vouchers

$client = Unifi::clients()->findByMac('aa:bb:cc:dd:ee:01');

$result = Unifi::clients()->authorizeGuest($client->id, minutes: 1440, megabytes: 2048, downKbps: 10000, upKbps: 2000);
$result->granted->expiresAt;                                  // CarbonImmutable (UTC)

Unifi::clients()->unauthorizeGuest($client->id);

foreach (Unifi::clients()->guests() as $guest) {              // LazyCollection over all pages
    $guest->isAuthorizedGuest();
}

Unifi::info()->applicationVersion;                            // "9.1.120"
Unifi::sites()->all();                                        // LazyCollection<Site>
Unifi::forSite('hotel2')->devices()->list();                  // short name or UUID

foreach (Unifi::devices()->all() as $device) {
    if (! $device->isOnline()) {
        // alert
    }
}

Unifi::devices()->statistics($deviceId)->cpuUtilizationPct;
Unifi::devices()->restart($deviceId);
Unifi::devices()->powerCyclePort($switchId, portIdx: 7);     // reboot a PoE access point
Unifi::devices()->pending();                                  // devices waiting for adoption
Unifi::devices()->adopt('94:2a:6f:26:c6:ca');

Unifi::networks()->list()->items;                             // Network (name, management, vlanId, ...)
Unifi::networks()->wans();                                    // ['uuid' => 'Internet 1']
Unifi::wifi()->all();                                         // WifiBroadcast (SSIDs)
Unifi::wifi()->disable($broadcastId);                         // reads the definition and sends it back
Unifi::wifi()->update($broadcastId, $definition);            // full definition, as in the official schema

use Beziworld\Unifi\Support\Filter;

$page = Unifi::vouchers()->list(
    Filter::and(Filter::eq('expired', false), Filter::like('name', 'Room*')),
    offset: 0,
    limit: 100,
);

$page->items; $page->totalCount; $page->hasMore(); $page->nextOffset();

$classic = Unifi::classic();                                  // configured site, or Unifi::classic('hotel2')

$classic->events()->recent(withinHours: 24, limit: 100);     // list<Event>
$classic->events()->alarms(archived: false);
$classic->events()->archiveAlarm($alarmId);

$classic->clients()->online();                                // list<ClassicClient>
$classic->clients()->block('aa:bb:cc:dd:ee:01');
$classic->clients()->reconnect('aa:bb:cc:dd:ee:01');
$classic->clients()->authorizeGuest('aa:bb:cc:dd:ee:01', minutes: 1440, downKbps: 10000);

$classic->vouchers()->create(minutes: 1440, count: 10, quota: 1, note: 'Lobby');
$classic->wlans()->setPassphrase($wlanId, 'Guest-' . now()->format('dmy'));
$classic->devices()->restart(['94:2a:6f:26:c6:ca']);
$classic->sites()->health();

Unifi::using([
    'url' => $hotel->unifi_url,
    'api_key' => decrypt($hotel->unifi_api_key),
    'site' => $hotel->unifi_site,
    'verify_ssl' => false,
])->vouchers()->forStay('Room 1', $checkout);

// Any endpoint of the official API; {siteId} is replaced with the current site UUID
Unifi::request('GET', '/v1/sites/{siteId}/firewall/policies');
Unifi::request('POST', '/v1/sites/{siteId}/acl-rules', body: [...]);

// Any classic endpoint of the current site
Unifi::classic()->request('GET', 'stat/health');
bash
php artisan unifi:check
bash
php artisan unifi:vouchers "Room 204" --minutes=4320 --guests=2 --connection=hotel-krakow