{"id":18783656,"url":"https://github.com/tomkyle/kurzelinks","last_synced_at":"2025-04-13T12:11:51.527Z","repository":{"id":255158594,"uuid":"848734815","full_name":"tomkyle/KurzeLinks","owner":"tomkyle","description":"Link shortener using the kurzelinks.de API. Supports PSR-6 caches and rate limits.","archived":false,"fork":false,"pushed_at":"2025-01-21T11:42:45.000Z","size":123,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T12:11:46.531Z","etag":null,"topics":["link-shortener","link-shortener-api","shortener"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomkyle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-28T10:04:53.000Z","updated_at":"2025-03-27T11:32:44.000Z","dependencies_parsed_at":"2024-08-28T11:32:16.714Z","dependency_job_id":"e2711530-5474-4042-b42d-a15799c70d29","html_url":"https://github.com/tomkyle/KurzeLinks","commit_stats":null,"previous_names":["tomkyle/kurzelinks","tomkyle/tomkyle-kurzelinks"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomkyle%2FKurzeLinks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomkyle%2FKurzeLinks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomkyle%2FKurzeLinks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomkyle%2FKurzeLinks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomkyle","download_url":"https://codeload.github.com/tomkyle/KurzeLinks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710450,"owners_count":21149191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["link-shortener","link-shortener-api","shortener"],"created_at":"2024-11-07T20:40:01.568Z","updated_at":"2025-04-13T12:11:51.502Z","avatar_url":"https://github.com/tomkyle.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tomkyle/kurzelinks\n\n**tomkyle/kurzelinks** is a PHP library designed to create short links using the [kurzelinks.de](https://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.\n\n[![Packagist](https://img.shields.io/packagist/v/tomkyle/kurzelinks.svg?style=flat)](https://packagist.org/packages/tomkyle/kurzelinks)\n[![PHP version](https://img.shields.io/packagist/php-v/tomkyle/kurzelinks.svg)](https://packagist.org/packages/tomkyle/kurzelinks)\n[![Tests](https://github.com/tomkyle/KurzeLinks/actions/workflows/php.yml/badge.svg)](https://github.com/tomkyle/KurzeLinks/actions/workflows/php.yml)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [GuzzleKurzeLinks](#guzzlekurzelinks)\n  - [Psr18KurzeLinks](#psr18kurzelinks)\n  - [RateLimitKurzeLinks](#ratelimitkurzelinks)\n  - [Psr6CacheKurzeLinks](#psr6cachekurzelinks)\n  - [PassthroughKurzeLinks](#passthroughkurzelinks)\n  - [CallableKurzeLinks](#callablekurzelinks)\n- [Best Practice: Usage Recommendation](#best-practice-usage-recommendation)\n- [Interface](#interface)\n  - [KurzeLinksInterface](#kurzelinksinterface)\n\n## Installation\n\nYou can install this library via Composer:\n\n```bash\ncomposer require tomkyle/kurzelinks\n```\n\n## Usage\n\n### GuzzleKurzeLinks\n\nThe `GuzzleKurzeLinks` class is an implementation of `KurzeLinksInterface` that uses GuzzleHTTP to interact with the [KurzeLinks.de API.](https://kurzelinks.de/kurz-url-api)\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\GuzzleKurzeLinks;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n$kurzeLinks = new GuzzleKurzeLinks($api, $key);\n\n$shortUrl = $kurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n```\n\n### Psr18KurzeLinks\n\nThe `Psr18KurzeLinks` class is an implementation of `KurzeLinksInterface` that uses a PSR-18 compliant HTTP client to interact with the [KurzeLinks.de API.](https://kurzelinks.de/kurz-url-api)\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\Psr18KurzeLinks;\nuse Psr\\Http\\Client\\ClientInterface;\nuse Psr\\Http\\Message\\RequestFactoryInterface;\nuse Psr\\Http\\Message\\StreamFactoryInterface;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n\n// Assume you have a PSR-18 compliant HTTP client, \n// and PSR-17 request and stream factories\n$httpClient = new YourPsr18Client();\n$requestFactory = new YourRequestFactory();\n$streamFactory = new YourStreamFactory();\n\n$kurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);\n\n$shortUrl = $kurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n```\n\n### RateLimitKurzeLinks\n\nThe `RateLimitKurzeLinks` class is a decorator for any `KurzeLinksInterface` implementation. It introduces a rate limit by pausing execution between requests.\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\Psr18KurzeLinks;\nuse tomkyle\\KurzeLinks\\RateLimitKurzeLinks;\nuse Psr\\Http\\Client\\ClientInterface;\nuse Psr\\Http\\Message\\RequestFactoryInterface;\nuse Psr\\Http\\Message\\StreamFactoryInterface;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n\n// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory\n$httpClient = new YourPsr18Client();\n$requestFactory = new YourRequestFactory();\n$streamFactory = new YourStreamFactory();\n\n$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);\n$rateLimitedKurzeLinks = new RateLimitKurzeLinks($innerKurzeLinks, 4000); // 4000ms sleep\n\n$shortUrl = $rateLimitedKurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n```\n\n### Psr6CacheKurzeLinks\n\nThe `Psr6CacheKurzeLinks` class is a decorator that caches the results of the `create` method using a PSR-6 compatible cache pool.\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\Psr18KurzeLinks;\nuse tomkyle\\KurzeLinks\\Psr6CacheKurzeLinks;\nuse Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter;\nuse Psr\\Http\\Client\\ClientInterface;\nuse Psr\\Http\\Message\\RequestFactoryInterface;\nuse Psr\\Http\\Message\\StreamFactoryInterface;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n\n// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory\n$httpClient = new YourPsr18Client();\n$requestFactory = new YourRequestFactory();\n$streamFactory = new YourStreamFactory();\n\n$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);\n$cachePool = new FilesystemAdapter();\n\n$cachedKurzeLinks = new Psr6CacheKurzeLinks($innerKurzeLinks, $cachePool);\n\n$shortUrl = $cachedKurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL, possibly from cache\n```\n\n### PassthroughKurzeLinks\n\nThe `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.\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\PassthroughKurzeLinks;\n\n$passthroughKurzeLinks = new PassthroughKurzeLinks();\n\n$shortUrl = $passthroughKurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the original URL: https://example.com\n```\n\n### CallableKurzeLinks\n\nThe `CallableKurzeLinks` class is a decorator that allows a `KurzeLinksInterface` implementation to be invoked directly as a callable.\n\n#### Example\n\n```php\nuse tomkyle\\KurzeLinks\\CallableKurzeLinks;\nuse tomkyle\\KurzeLinks\\Psr18KurzeLinks;\nuse Psr\\Http\\Client\\ClientInterface;\nuse Psr\\Http\\Message\\RequestFactoryInterface;\nuse Psr\\Http\\Message\\StreamFactoryInterface;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n\n// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory\n$httpClient = new YourPsr18Client();\n$requestFactory = new YourRequestFactory();\n$streamFactory = new YourStreamFactory();\n\n$innerKurzeLinks = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);\n$callableKurzeLinks = new CallableKurzeLinks($innerKurzeLinks);\n\n// Use as callable\n$shortUrl = $callableKurzeLinks('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n\n// Use create method directly\n$shortUrl = $callableKurzeLinks-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n```\n\n## Best Practice: Usage Recommendation\n\nTo ensure efficient usage of the [kurzelinks.de](https://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.\n\n### Why Use Rate Limiting?\n\nThe `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.\n\n### Why Use Caching?\n\nThe `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.\n\n### Recommended Implementation\n\nBelow is a recommended setup that combines both `RateLimitKurzeLinks` and `Psr6CacheKurzeLinks`:\n\n```php\nuse tomkyle\\KurzeLinks\\Psr18KurzeLinks;\nuse tomkyle\\KurzeLinks\\RateLimitKurzeLinks;\nuse tomkyle\\KurzeLinks\\Psr6CacheKurzeLinks;\nuse Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter;\nuse Psr\\Http\\Client\\ClientInterface;\nuse Psr\\Http\\Message\\RequestFactoryInterface;\nuse Psr\\Http\\Message\\StreamFactoryInterface;\n\n$api = 'https://kurzelinks.de/api';\n$key = 'your_api_key';\n\n// Assume you have a PSR-18 compliant HTTP client, request factory, and stream factory\n$httpClient = new YourPsr18Client();\n$requestFactory = new YourRequestFactory();\n$streamFactory = new YourStreamFactory();\n\n$kurze_links = new Psr18KurzeLinks($api, $key, $httpClient, $requestFactory, $streamFactory);\n\n// Wrap the cached implementation with rate limiting\n$rate_limited = new RateLimitKurzeLinks(kurze_links, 4000); // 4000ms sleep\n\n// Create a PSR-6 cache pool (e.g., using Symfony's FilesystemAdapter)\n// and wrap the rate-limited implementation with caching\n$cachePool = new FilesystemAdapter();\n$cached = new Psr6CacheKurzeLinks($rate_limited, $cachePool);\n\n// Use the cached, rate-limited implementation\n$shortUrl = $cached-\u003ecreate('https://example.com');\necho $shortUrl;  // Outputs the shortened URL\n```\n\n## Interface\n\n### KurzeLinksInterface\n\nThe `KurzeLinksInterface` defines the contract for creating short links.\n\n#### Method\n\n- `create(string $url): string`  \n  Creates a short link representation for the given URL.\n\n#### Example\n\nAny class implementing this interface must define the `create` method:\n\n```php\nuse tomkyle\\KurzeLinks\\KurzeLinksInterface;\n\nclass MyKurzeLinks implements KurzeLinksInterface\n{\n    public function create(string $url): string\n    {\n        // Your implementation here\n    }\n}\n```\n\n## License\n\nThis library is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomkyle%2Fkurzelinks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomkyle%2Fkurzelinks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomkyle%2Fkurzelinks/lists"}