teracrafts/huefy-sdk-php
Composer 安装命令:
composer require teracrafts/huefy-sdk-php
包简介
Huefy - PHP SDK
README 文档
README
Official PHP SDK for Huefy — transactional email delivery made simple.
Installation
composer require teracrafts/huefy-sdk
To use a custom PSR-18 HTTP client instead of the bundled Guzzle adapter:
composer require teracrafts/huefy-sdk psr/http-client your-psr18-client
Requirements
- PHP 8.1+
guzzlehttp/guzzle(installed automatically, or bring your own PSR-18 client)
Quick Start
use Teracrafts\Huefy\HuefyEmailClient; use Teracrafts\Huefy\Models\SendEmailRequest; use Teracrafts\Huefy\Models\SendEmailRecipient; $client = new HuefyEmailClient([ 'apiKey' => 'sdk_your_api_key', ]); $response = $client->sendEmail(new SendEmailRequest( templateKey: 'welcome-email', data: ['firstName' => 'Alice', 'trialDays' => 14], recipient: new SendEmailRecipient( email: 'alice@example.com', type: 'cc', data: ['locale' => 'en'], ), )); echo 'Email ID: ' . $response->data->emailId . PHP_EOL;
Key Features
- PSR-18 compatible — swap in any PSR-18 HTTP client
- PSR-3 logger — plug in any PSR-3 logger (Monolog, etc.)
- PHP 8.1 enums —
EmailProvideris a native enum - Named arguments — all constructors support PHP 8 named argument syntax
- Retry with exponential backoff — configurable attempts, base delay, ceiling, and jitter
- Circuit breaker — opens after 5 consecutive failures, probes after 30 s
- HMAC-SHA256 signing — optional request signing for additional integrity verification
- Key rotation — primary + secondary API key with seamless failover
- Rate limit callbacks —
onRateLimitUpdatefires whenever rate-limit headers change - PII detection — warns when template variables contain sensitive field patterns
Configuration Reference
| Parameter | Default | Description |
|---|---|---|
apiKey |
— | Required. Must have prefix sdk_, srv_, or cli_ |
baseUrl |
https://api.huefy.dev/api/v1/sdk |
Override the API base URL |
timeout |
30.0 |
Request timeout in seconds |
retryConfig->maxAttempts |
3 |
Total attempts including the first |
retryConfig->baseDelay |
0.5 |
Exponential backoff base delay (seconds) |
retryConfig->maxDelay |
10.0 |
Maximum backoff delay (seconds) |
retryConfig->jitter |
0.2 |
Random jitter factor (0–1) |
circuitBreakerConfig->failureThreshold |
5 |
Consecutive failures before circuit opens |
circuitBreakerConfig->resetTimeout |
30.0 |
Seconds before half-open probe |
logger |
null |
PSR-3 logger instance |
secondaryApiKey |
null |
Backup key used during key rotation |
enableRequestSigning |
false |
Enable HMAC-SHA256 request signing |
onRateLimitUpdate |
null |
Callable fired on rate-limit header changes |
Bulk Email
use Teracrafts\Huefy\Models\BulkRecipient; use Teracrafts\Huefy\Models\SendBulkEmailsRequest; $bulk = $client->sendBulkEmails(new SendBulkEmailsRequest( templateKey: 'promo', recipients: [ new BulkRecipient(email: 'bob@example.com'), new BulkRecipient(email: 'carol@example.com'), ] )); echo "Sent: {$bulk->data->successCount}, Failed: {$bulk->data->failureCount}" . PHP_EOL;
Error Handling
use Teracrafts\Huefy\Errors\ErrorCode; use Teracrafts\Huefy\Errors\HuefyException; try { $response = $client->sendEmail($request); echo 'Delivered: ' . $response->data->emailId . PHP_EOL; } catch (HuefyException $e) { if ($e->getErrorCode() === ErrorCode::AUTHENTICATION_ERROR) { echo 'Invalid API key' . PHP_EOL; } elseif ($e->getErrorCode() === ErrorCode::RATE_LIMIT_ERROR) { echo 'Rate limited' . PHP_EOL; } elseif ($e->getErrorCode() === ErrorCode::CIRCUIT_BREAKER_OPEN) { echo 'Circuit open — service unavailable, backing off' . PHP_EOL; } else { echo "Huefy error [{$e->getErrorCode()}]: {$e->getMessage()}" . PHP_EOL; } }
Error Code Reference
| Exception | Code | Meaning |
|---|---|---|
HuefyInitException |
1001 | Client failed to initialise |
HuefyAuthException |
1102 | API key rejected |
HuefyNetworkException |
1201 | Upstream request failed |
HuefyCircuitOpenException |
1301 | Circuit breaker tripped |
HuefyRateLimitException |
2003 | Rate limit exceeded |
HuefyTemplateMissingException |
2005 | Template key not found |
Health Check
$health = $client->healthCheck(); if (($health['data']['status'] ?? null) !== 'healthy') { error_log('Huefy degraded: ' . ($health['data']['status'] ?? 'unknown')); }
Local Development
The PHP SDK does not resolve HUEFY_MODE automatically. To match the local Caddy setup, set baseUrl to https://api.huefy.on/api/v1/sdk. To bypass Caddy and hit the raw app port directly, use http://localhost:8080/api/v1/sdk instead:
$client = new HuefyEmailClient([ 'apiKey' => 'sdk_local_key', 'baseUrl' => 'https://api.huefy.on/api/v1/sdk', ]);
Namespace Compatibility
The canonical PHP namespace is Teracrafts\Huefy\.... Existing Huefy\... references remain supported through the bundled compatibility aliases.
Developer Guide
Full documentation, advanced patterns, and provider configuration are in the PHP Developer Guide.
License
MIT
teracrafts/huefy-sdk-php 适用场景与选型建议
teracrafts/huefy-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「template」 「email」 「sdk」 「transactional」 「huefy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 teracrafts/huefy-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 teracrafts/huefy-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 teracrafts/huefy-sdk-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Simple ASCII output of array data
E2E Studios PHP Framework adaptation of leafsphp/blade package
Laravel contact us form package to send email and save to database
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-04