tomkyle/kurzelinks
Composer 安装命令:
composer require tomkyle/kurzelinks
包简介
Link shortener using the kurzelinks.de API. Supports PSR-6 caches and rate limits.
README 文档
README
tomkyle/kurzelinks is a PHP library designed to create short links using the kurzelinks.de service. This library provides different implementations of the KurzeLinksInterface to allow developers to integrate and extend the short link creation functionality with ease.
Table of Contents
Installation
You can install this library via Composer:
composer require tomkyle/kurzelinks
Usage
GuzzleKurzeLinks
The GuzzleKurzeLinks class is an implementation of KurzeLinksInterface that uses GuzzleHTTP to interact with the KurzeLinks.de API.
Example
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
The Psr18KurzeLinks class is an implementation of KurzeLinksInterface that uses a PSR-18 compliant HTTP client to interact with the KurzeLinks.de API.
Example
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
The RateLimitKurzeLinks class is a decorator for any KurzeLinksInterface implementation. It introduces a rate limit by pausing execution between requests.
Example
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
The Psr6CacheKurzeLinks class is a decorator that caches the results of the create method using a PSR-6 compatible cache pool.
Example
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
The PassthroughKurzeLinks class is a simple implementation of KurzeLinksInterface that returns the original URL without shortening it. This can be useful for testing or as a default behavior.
Example
use tomkyle\KurzeLinks\PassthroughKurzeLinks; $passthroughKurzeLinks = new PassthroughKurzeLinks(); $shortUrl = $passthroughKurzeLinks->create('https://example.com'); echo $shortUrl; // Outputs the original URL: https://example.com
CallableKurzeLinks
The CallableKurzeLinks class is a decorator that allows a KurzeLinksInterface implementation to be invoked directly as a callable.
Example
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
Best Practice: Usage Recommendation
To ensure efficient usage of the kurzelinks.de API, especially considering the restrictive rate limits, it is highly recommended to utilize the RateLimitKurzeLinks and Psr6CacheKurzeLinks decorators together. These wrappers help manage API requests effectively by limiting the rate at which requests are sent and caching responses to avoid unnecessary duplicate requests.
Why Use Rate Limiting?
The RateLimitKurzeLinks decorator enforces a delay between API requests. This is crucial when working with services that impose strict limits on the number of requests allowed per hour. By introducing a delay, you reduce the risk of exceeding these limits and receiving errors from the API due to overuse.
Why Use Caching?
The Psr6CacheKurzeLinks decorator caches the results of the create method. This is particularly useful when the same URL is shortened multiple times within a short period. Instead of making multiple API requests, the cached result is returned, which saves on API quota and improves performance by reducing network latency.
Recommended Implementation
Below is a recommended setup that combines both RateLimitKurzeLinks and 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
Interface
KurzeLinksInterface
The KurzeLinksInterface defines the contract for creating short links.
Method
create(string $url): string
Creates a short link representation for the given URL.
Example
Any class implementing this interface must define the create method:
use tomkyle\KurzeLinks\KurzeLinksInterface; class MyKurzeLinks implements KurzeLinksInterface { public function create(string $url): string { // Your implementation here } }
License
This library is licensed under the MIT License. See the LICENSE file for more details.
tomkyle/kurzelinks 适用场景与选型建议
tomkyle/kurzelinks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「url」 「shortener」 「shortening」 「url shortener」 「link shortener」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tomkyle/kurzelinks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tomkyle/kurzelinks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tomkyle/kurzelinks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
url shorten package using Laravel
A Laravel package to shorten urls
Easy URL rewrites in your Laravel application
Powerful URL shortening tools in Laravel
Laravel 7 local url shortener
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-28