PHP code example of tomkyle / kurzelinks

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

    

tomkyle / kurzelinks example snippets


use tomkyle\KurzeLinks\GuzzleKurzeLinks;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';
$kurzeLinks = new GuzzleKurzeLinks($api, $key);

$shortUrl = $kurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

use tomkyle\KurzeLinks\Psr18KurzeLinks;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';

// Assume you have a PSR-18 compliant HTTP client, 
// and PSR-17 request and stream factories
$httpClient = new YourPsr18Client();
$requestFactory = new YourRequestFactory();
$streamFactory = new YourStreamFactory();

$kurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);

$shortUrl = $kurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

use tomkyle\KurzeLinks\Psr18KurzeLinks;
use tomkyle\KurzeLinks\RateLimitKurzeLinks;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';

// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory
$httpClient = new YourPsr18Client();
$requestFactory = new YourRequestFactory();
$streamFactory = new YourStreamFactory();

$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);
$rateLimitedKurzeLinks = new RateLimitKurzeLinks($innerKurzeLinks, 4000); // 4000ms sleep

$shortUrl = $rateLimitedKurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

use tomkyle\KurzeLinks\Psr18KurzeLinks;
use tomkyle\KurzeLinks\Psr6CacheKurzeLinks;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';

// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory
$httpClient = new YourPsr18Client();
$requestFactory = new YourRequestFactory();
$streamFactory = new YourStreamFactory();

$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);
$cachePool = new FilesystemAdapter();

$cachedKurzeLinks = new Psr6CacheKurzeLinks($innerKurzeLinks, $cachePool);

$shortUrl = $cachedKurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL, possibly from cache

use tomkyle\KurzeLinks\PassthroughKurzeLinks;

$passthroughKurzeLinks = new PassthroughKurzeLinks();

$shortUrl = $passthroughKurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the original URL: https://example.com

use tomkyle\KurzeLinks\CallableKurzeLinks;
use tomkyle\KurzeLinks\Psr18KurzeLinks;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';

// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory
$httpClient = new YourPsr18Client();
$requestFactory = new YourRequestFactory();
$streamFactory = new YourStreamFactory();

$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);
$callableKurzeLinks = new CallableKurzeLinks($innerKurzeLinks);

// Use as callable
$shortUrl = $callableKurzeLinks('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

// Use create method directly
$shortUrl = $callableKurzeLinks->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

use tomkyle\KurzeLinks\Psr18KurzeLinks;
use tomkyle\KurzeLinks\RateLimitKurzeLinks;
use tomkyle\KurzeLinks\Psr6CacheKurzeLinks;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

$api = 'https://kurzelinks.de/api';
$key = 'your_api_key';

// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory
$httpClient = new YourPsr18Client();
$requestFactory = new YourRequestFactory();
$streamFactory = new YourStreamFactory();

$kurze_links = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);

// Wrap the cached implementation with rate limiting
$rate_limited = new RateLimitKurzeLinks(kurze_links, 4000); // 4000ms sleep

// Create a PSR-6 cache pool (e.g., using Symfony's FilesystemAdapter)
// and wrap the rate-limited implementation with caching
$cachePool = new FilesystemAdapter();
$cached = new Psr6CacheKurzeLinks($rate_limited, $cachePool);

// Use the cached, rate-limited implementation
$shortUrl = $cached->create('https://example.com');
echo $shortUrl;  // Outputs the shortened URL

use tomkyle\KurzeLinks\KurzeLinksInterface;

class MyKurzeLinks implements KurzeLinksInterface
{
    public function create(string $url): string
    {
        // Your implementation here
    }
}