ShortyyDocs

PHP REST API Integration

PHP integration using native cURL and GuzzleHttp for Laravel, Symfony, and WordPress applications.

PHP REST API Integration

Shortyy integrates cleanly into Laravel, Symfony, or vanilla PHP applications.


Using Native cURL

php
<?php

function createShortyyLink(string $url, string $expiresIn = '2h'): array {
    $apiKey = getenv('SHORTYY_API_KEY');
    $endpoint = 'https://api.shortyy.com/v1/links';

    $payload = json_encode([
        'url' => $url,
        'expires_in' => $expiresIn,
    ]);

    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
        'Accept: application/json',
    ]);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode === 429) {
        throw new Exception("Shortyy token rate limit exceeded.");
    }

    if ($httpCode !== 201) {
        throw new Exception("Shortyy API request failed with code $httpCode: $response");
    }

    return json_decode($response, true)['link'];
}

// Example:
$link = createShortyyLink('https://php.net', '1d');
echo "Short URL: " . $link['short_url'] . PHP_EOL;