tomkyle / kurzelinks
使用kurzelinks.de API的链接缩短工具。支持PSR-6缓存和速率限制。
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.8
- psr/cache: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- guzzlehttp/psr7: ^2.7
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.0
- rector/rector: ^1.0
- tomkyle/find-run-test: ^1.0
This package is auto-updated.
Last update: 2024-08-29 09:10:25 UTC
README
tomkyle/kurzelinks 是一个PHP库,旨在使用 kurzelinks.de 服务创建短链接。此库提供了不同的 KurzeLinksInterface
实现,以允许开发人员轻松集成和扩展短链接创建功能。
目录
安装
您可以通过Composer安装此库
composer require tomkyle/kurzelinks
使用
GuzzleKurzeLinks
GuzzleKurzeLinks
类是 KurzeLinksInterface
的一个实现,它使用GuzzleHTTP与 KurzeLinks.de API 交互。
示例
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
Psr18KurzeLinks
Psr18KurzeLinks
类是 KurzeLinksInterface
的一个实现,它使用符合PSR-18的HTTP客户端与 KurzeLinks.de API 交互。
示例
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
RateLimitKurzeLinks
RateLimitKurzeLinks
类是任何 KurzeLinksInterface
实现的装饰器。它通过在请求之间暂停执行来引入速率限制。
示例
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
Psr6CacheKurzeLinks
Psr6CacheKurzeLinks
类是一个装饰器,它使用PSR-6兼容的缓存池来缓存 create
方法的结果。
示例
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
PassthroughKurzeLinks
PassthroughKurzeLinks
类是一个简单的 KurzeLinksInterface
实现,它返回原始URL而不对其进行缩短。这可以用于测试或作为默认行为。
示例
use tomkyle\KurzeLinks\PassthroughKurzeLinks; $passthroughKurzeLinks = new PassthroughKurzeLinks(); $shortUrl = $passthroughKurzeLinks->create('https://example.com'); echo $shortUrl; // Outputs the original URL: https://example.com
CallableKurzeLinks
CallableKurzeLinks
类是一个装饰器,它允许直接将 KurzeLinksInterface
实现作为可调用对象调用。
示例
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
最佳实践:使用建议
为了确保高效地使用 kurzelinks.de API,特别是考虑到限制性的速率限制,强烈建议一起使用 RateLimitKurzeLinks
和 Psr6CacheKurzeLinks
装饰器。这些包装器通过限制请求发送的速率和缓存响应来有效地管理API请求,以避免不必要的重复请求。
为什么要使用速率限制?
RateLimitKurzeLinks
装饰器强制在API请求之间引入延迟。这对于处理对每小时允许的请求数量施加严格限制的服务至关重要。通过引入延迟,您可以减少超出这些限制的风险,从而避免由于过度使用而导致的API错误。
为什么要使用缓存?
Psr6CacheKurzeLinks
装饰器缓存 create
方法的结果。这在短时间内多次缩短同一URL时非常有用。而不是多次进行API请求,缓存的结果将被返回,从而节省API配额并提高性能,减少网络延迟。
推荐实现
以下是一个推荐的设置,结合了 RateLimitKurzeLinks
和 Psr6CacheKurzeLinks
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
接口
KurzeLinksInterface
KurzeLinksInterface
定义了创建短链接的合约。
方法
create(string $url): string
为给定的URL创建短链接表示。
示例
实现此接口的任何类都必须定义 create
方法
use tomkyle\KurzeLinks\KurzeLinksInterface; class MyKurzeLinks implements KurzeLinksInterface { public function create(string $url): string { // Your implementation here } }
许可证
本库采用MIT许可证。有关详细信息,请参阅LICENSE文件。