<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Dub\Dub;
use Dub\Models\Operations;
// Initialize the Dub SDK with your API key
$dub = Dub::builder()
->setSecurity(getenv('DUB_API_KEY')) // optional, defaults to DUB_API_KEY
->build();
// Create a new link
$request = new Operations\CreateLinkRequestBody(
url: 'https://google.com',
);
try {
$response = $dub->links->create($request);
if ($response->linkSchema !== null) {
echo $response->linkSchema->shortLink; // e.g. https://dub.sh/abc123
}
// Get analytics for the link
$analyticsRequest = new Operations\RetrieveAnalyticsRequest();
$analyticsRequest->linkId = $response->linkSchema->id;
$analyticsRequest->interval = Operations\Interval::ThirtyD;
$analyticsRequest->groupBy = Operations\GroupBy::Timeseries;
$analyticsResponse = $dub->analytics->retrieve($analyticsRequest);
if ($analyticsResponse->oneOf !== null) {
print_r($analyticsResponse->oneOf); // e.g. [{ start: "2024-01-01", clicks: 100 }]
}
} catch (Throwable $e) {
// handle exception
}