PHP code example of vaslv / brevity-php-sdk

1. Go to this page and download the library: Download vaslv/brevity-php-sdk 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/ */

    

vaslv / brevity-php-sdk example snippets


use Vaslv\Brevity\BrevityClient;

$client = new BrevityClient([
    'base_uri' => 'https://brevity.example.com', // the technical host, not a short-link domain
    'token' => getenv('BREVITY_TOKEN'),
]);

$link = $client->createSimpleLink('https://example.com/landing');
echo $link->getUrl(); // https://short.example.com/AbC12345

use Vaslv\Brevity\DTO\CreateLinkCondition;
use Vaslv\Brevity\DTO\CreateLinkRequest;
use Vaslv\Brevity\DTO\CreateLinkRule;
use Vaslv\Brevity\DTO\CreateLinkVariant;

$request = new CreateLinkRequest(
    null,                            // domain (null → the default domain)
    'Spring campaign',               // title
    true,                            // forward_query
    ['click_id' => '{{click.id}}'],  // callback_data template
    [
        // Rules are tried in order; the first whose conditions all match wins.
        new CreateLinkRule('https://example.com/sale', [
            new CreateLinkCondition('time_before', ['before' => '2026-03-05T10:00:00+00:00']),
            new CreateLinkCondition('device', ['device' => 'mobile']),
        ]),
        // A/B split: weights 1:3, sticky per visitor.
        new CreateLinkRule('https://example.com/control', [], null, [
            new CreateLinkVariant('https://example.com/a', 1, 'A'),
            new CreateLinkVariant('https://example.com/b', 3, 'B'),
        ]),
        // Unconditional fallback.
        new CreateLinkRule('https://example.com/home'),
    ]
);

$link = $client->createLink($request);

$request = new CreateLinkRequest(
    null, null, null, null,
    [new CreateLinkRule('https://example.com/landing')],
    null, null,
    new DateTimeImmutable('2026-08-01T00:00:00+00:00'), // valid_since
    new DateTimeImmutable('2026-09-01T00:00:00+00:00'), // valid_until (inclusive)
    100                                                 // max_clicks (bots count too)
);

// Explicit domain (must exist in the registry):
$client->createSimpleLink('https://example.com/x', 'go.example.com');

// Auto-picked domain: 'random' / 'round_robin' / 'coldest', optionally within a group:
$request = new CreateLinkRequest(
    null, null, null, null,
    [new CreateLinkRule('https://example.com/x')],
    'round_robin',
    'campaigns'
);

// Registries:
$domains = $client->listDomains();     // Domain[]; or listDomains('campaigns')
$groups = $client->listDomainGroups(); // DomainGroup[]

$link = $client->getLink('AbC12345');  // needs the links:read ability

$link->getClicks()->getTotal();        // all clicks, bots 

use Vaslv\Brevity\DTO\UpdateLinkRequest;

$patch = (new UpdateLinkRequest)
    ->setValidUntil(new DateTimeImmutable('2026-12-31T23:59:59+00:00'))
    ->setMaxClicks(null);              // an explicit null clears the budget

$client->updateLink('AbC12345', $patch); // needs the links:update ability

use Vaslv\Brevity\Exceptions\ApiException;
use Vaslv\Brevity\Exceptions\RateLimitException;
use Vaslv\Brevity\Exceptions\TransportException;
use Vaslv\Brevity\Exceptions\ValidationException;

try {
    $link = $client->createLink($request);
} catch (ValidationException $e) {
    $errors = $e->getErrors();    // field => messages[]
} catch (RateLimitException $e) {
    $wait = $e->getRetryAfter();  // seconds, or null
} catch (ApiException $e) {
    $type = $e->getProblemType(); // stable machine code, or null
} catch (TransportException $e) {
    // network failure after the configured retries
}

$link = Brevity::createSimpleLink('https://example.com/landing');
bash
composer