shorter/sdk
Composer 安装命令:
composer require shorter/sdk
包简介
PHP SDK for the shorter.sh URL shortener
README 文档
README
PHP SDK for the shorter.sh URL shortener. Requires PHP >=8.1 and Guzzle 7.
Installation
composer require shorter/sdk
Quick Start
use Shorter\Sdk\ShorterClient; $client = new ShorterClient( api_key: 'sk_your_key_here', // or set SHORTER_API_KEY env var ); // Shorten a URL $result = $client->shorten('https://example.com'); echo $result->short_url; // https://shorter.sh/xK9mP2 // List your URLs $list = $client->list(page: 1, limit: 50); foreach ($list->urls as $url) { echo "{$url->short_url} → {$url->original_url} ({$url->click_count} clicks)\n"; } echo "Total clicks: {$list->total_clicks}\n"; // Delete a URL $client->delete('xK9mP2');
Authentication
The API key is resolved in order:
api_keyconstructor parameterSHORTER_API_KEYenvironment variable
The key must start with sk_.
Analytics
// Overview analytics $overview = $client->analytics->overview( start: '2024-01-01', end: '2024-01-31', ); echo "Total clicks: {$overview->total_clicks}\n"; echo "Unique visitors: {$overview->unique_visitors}\n"; // Top URLs foreach ($overview->top_urls as $url) { echo "{$url->short_url}: {$url->clicks} clicks\n"; } // Breakdowns (country, device, browser, os, referrer) foreach ($overview->country_breakdown as $item) { echo "{$item->value}: {$item->clicks} ({$item->percentage}%)\n"; } // Per-URL analytics $stats = $client->analytics->url('xK9mP2'); echo "Clicks: {$stats->summary->total_clicks}\n"; echo "Top country: {$stats->summary->top_country}\n"; // Per-URL with a specific breakdown dimension $stats = $client->analytics->url('xK9mP2', dimension: 'country'); echo "Breakdown: {$stats->breakdown->dimension}\n"; foreach ($stats->breakdown->data as $item) { echo " {$item->value}: {$item->clicks}\n"; } // Detailed analytics (all breakdowns at once) $detail = $client->analytics->url('xK9mP2', detail: true); echo "Original URL: {$detail->url->original_url}\n"; foreach ($detail->breakdowns as $dimension => $breakdown) { echo "{$dimension}: {$breakdown->total} total\n"; }
Dimensions
Available breakdown dimensions: country, device_type, browser, os, referrer_domain, language
Error Handling
use Shorter\Sdk\Exceptions\ValidationException; use Shorter\Sdk\Exceptions\RateLimitException; use Shorter\Sdk\Exceptions\NetworkException; use Shorter\Sdk\Exceptions\ShorterException; try { $client->shorten('not-a-url'); } catch (ValidationException $e) { echo $e->getMessage(); // "Invalid URL" echo $e->errorCode; // "VALIDATION_ERROR" echo $e->status; // 400 } catch (RateLimitException $e) { // Back off and retry } catch (NetworkException $e) { // Connection failed } catch (ShorterException $e) { // Any other API error }
Exception Classes
| Class | Status | Default Code |
|---|---|---|
ValidationException |
400 | VALIDATION_ERROR |
AuthenticationException |
401 | AUTH_REQUIRED |
ForbiddenException |
403 | FORBIDDEN |
NotFoundException |
404 | NOT_FOUND |
RateLimitException |
429 | RATE_LIMITED |
ServerException |
500 | SERVER_ERROR |
NetworkException |
0 | NETWORK_ERROR |
All extend ShorterException, which extends \RuntimeException.
Custom HTTP Client
Inject your own Guzzle client for proxies, timeouts, or testing:
use GuzzleHttp\Client; $client = new ShorterClient( api_key: 'sk_...', http_client: new Client([ 'timeout' => 10, 'proxy' => 'http://proxy:8080', ]), );
Requirements
- PHP >= 8.1
- Guzzle >= 7.0
License
MIT
shorter/sdk 适用场景与选型建议
shorter/sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sdk」 「URL-Shortener」 「short-url」 「link-shortener」 「shorter.sh」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shorter/sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shorter/sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shorter/sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package for creating shortened URLs for your web apps.
Laravel package for Enlace2 URL shortener service integration - Complete API wrapper with support for links, QR codes, campaigns, channels, domains, pixels, and overlays
Laravel package to interact with the URL Shortener API service
A fast and powerful URL Shortener.
SURL - Laravel Short Url
Powerful URL shoterning tools using different drivers for your Laravel projects
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15