PHP code example of linkskipper / sdk

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

    

linkskipper / sdk example snippets




use LinkSkipper\LinkSkipper;
use LinkSkipper\Exception\JobFailedException;
use LinkSkipper\Exception\TimeoutException;

Ef', maxWaitMs: 60000, pollIntervalMs: 2000);

    echo $link->targetUrl, PHP_EOL;
    printf("%s (%s) · %d credit(s) · cached=%s\n", $link->provider, $link->tier->value, $link->creditsCharged, $link->cached ? 'yes' : 'no');
} catch (JobFailedException $exception) {
    fwrite(STDERR, 'Could not resolve: ' . $exception->reason . PHP_EOL);
} catch (TimeoutException $exception) {
    fwrite(STDERR, 'Still pending after the deadline: ' . $exception->jobId . PHP_EOL);
}

use LinkSkipper\Config;
use LinkSkipper\LinkSkipper;
use LinkSkipper\Http\RetryPolicy;

$client = new LinkSkipper(new Config(
    apiKey: 'sk_live_...',
    baseUrl: 'https://linkskipper.app',
    timeoutMs: 30000,
    pollIntervalMs: 2000,
    maxWaitMs: 120000,
    retryPolicy: new RetryPolicy(maxAttempts: 3, initialDelayMs: 500, maxDelayMs: 8000, backoffFactor: 2.0),
));

use LinkSkipper\Config;
use LinkSkipper\Http\Psr18Transport;

$transport = new Psr18Transport($psr18Client, $psr17RequestFactory, $psr17StreamFactory);
$config = new Config(apiKey: 'sk_live_...', transport: $transport);

$result = $client->resolve('https://cuty.io/xyz', idempotencyKey: 'order-42');
if ($result->isDone()) {
    echo $result->targetUrl;
} else {
    printf("queued at position %d -> %s\n", $result->queuePosition, $result->pollUrl);
}

$account = $client->account();
printf("balance: %d, until: %s\n", $account->balance, $account->subscriptionUntil ?? 'none');

foreach ($client->providers() as $provider) {
    printf("%s (%s)\n", $provider->label, $provider->tier->value);
}

use LinkSkipper\Exception\RateLimitedException;
use LinkSkipper\Exception\OutOfCreditsException;

try {
    $client->resolveAndWait('https://exe.io/AbCdEf');
} catch (RateLimitedException $exception) {
    echo 'retry after ' . $exception->retryAfter() . ' seconds';
} catch (OutOfCreditsException $exception) {
    echo 'balance: ' . $exception->balance();
}

use LinkSkipper\Webhook;
use LinkSkipper\Enum\WebhookEventName;
use LinkSkipper\Exception\WebhookVerificationException;

$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_LINKSKIPPER_SIGNATURE'] ?? '';

try {
    $event = Webhook::verify($payload, $signature, getenv('LINKSKIPPER_WEBHOOK_SECRET'));

    if ($event->event === WebhookEventName::ResolveDone) {
        printf("Job %s -> %s\n", $event->jobId, $event->targetUrl);
    }
    http_response_code(204);
} catch (WebhookVerificationException $exception) {
    http_response_code(400);
}