shorter/sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shorter/sdk

最新稳定版本:v1.0.1

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:

  1. api_key constructor parameter
  2. SHORTER_API_KEY environment 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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固