phpmystic/retrofit-php
Composer 安装命令:
composer require phpmystic/retrofit-php
包简介
A type-safe HTTP client for PHP inspired by Square's Retrofit. Turn your HTTP API into a PHP interface using attributes.
关键字:
README 文档
README
A type-safe HTTP client for PHP, inspired by Square's Retrofit for Java/Kotlin.
Turn your HTTP API into a PHP interface using attributes.
Features
- ✅ Type-Safe API Definitions - Define APIs as PHP interfaces with attributes
- ✅ HTTP Methods - Support for GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
- ✅ URL Parameters - Path parameters, query parameters, and query maps
- ✅ Request Body - JSON, form-encoded, and multipart requests
- ✅ Headers - Static and dynamic headers
- ✅ Converters - JSON, XML, and custom converters
- ✅ DTO Hydration - Automatic object mapping with nested objects and field name mapping
- ✅ Async Requests - Promise-based async operations with Guzzle
- ✅ Retry Policies - Automatic retries with exponential backoff
- ✅ Response Caching - TTL-based caching with pluggable cache backends
- ✅ Interceptors - Request/response modification and logging
- ✅ Timeouts - Per-endpoint timeout configuration
- ✅ File Handling - Streaming uploads/downloads with progress tracking
- ✅ PSR-7 & PSR-18 Compliant - Full PSR standards support
- ✅ XML Support - Built-in XML serialization/deserialization
- ✅ Symfony Serializer - Advanced serialization with groups and contexts
Installation
composer require phpmystic/retrofit-php
Quick Start
1. Define your API interface
use Phpmystic\RetrofitPhp\Attributes\Http\GET; use Phpmystic\RetrofitPhp\Attributes\Http\POST; use Phpmystic\RetrofitPhp\Attributes\Parameter\Path; use Phpmystic\RetrofitPhp\Attributes\Parameter\Query; use Phpmystic\RetrofitPhp\Attributes\Parameter\Body; interface GitHubApi { #[GET('/users/{user}')] public function getUser(#[Path('user')] string $username): array; #[GET('/users/{user}/repos')] public function listRepos( #[Path('user')] string $username, #[Query('per_page')] int $perPage = 10, #[Query('sort')] string $sort = 'updated' ): array; #[POST('/repos/{owner}/{repo}/issues')] public function createIssue( #[Path('owner')] string $owner, #[Path('repo')] string $repo, #[Body] array $issue ): array; }
2. Create a Retrofit instance
use Phpmystic\RetrofitPhp\Retrofit; use Phpmystic\RetrofitPhp\Http\GuzzleHttpClient; use Phpmystic\RetrofitPhp\Converter\JsonConverterFactory; $retrofit = Retrofit::builder() ->baseUrl('https://api.github.com') ->client(GuzzleHttpClient::create(['timeout' => 30])) ->addConverterFactory(new JsonConverterFactory()) ->build(); // Or use TypedJsonConverterFactory for automatic DTO hydration use Phpmystic\RetrofitPhp\Converter\TypedJsonConverterFactory; $retrofit = Retrofit::builder() ->baseUrl('https://api.github.com') ->client(GuzzleHttpClient::create(['timeout' => 30])) ->addConverterFactory(new TypedJsonConverterFactory()) // Enables DTO hydration ->build();
3. Use your API
$github = $retrofit->create(GitHubApi::class); // GET request with path parameter $user = $github->getUser('octocat'); echo $user['name']; // "The Octocat" // GET request with query parameters $repos = $github->listRepos('octocat', perPage: 5, sort: 'stars'); // POST request with body $issue = $github->createIssue('owner', 'repo', [ 'title' => 'Bug report', 'body' => 'Something is broken', ]);
Documentation
Core Concepts
- HTTP Methods - GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
- URL Parameters - Path parameters, query parameters, and query maps
- Request Body - JSON, form-encoded, and multipart requests
- Headers - Static and dynamic header configuration
Configuration
- HTTP Client Configuration - Guzzle options and custom clients
- Converters - JSON, XML, Symfony Serializer, DTO hydration, and custom converters
- Timeouts - Per-endpoint timeout configuration
Advanced Features
- Async Requests - Promise-based async operations and parallel requests
- Retry Policies - Automatic retries with backoff strategies
- Response Caching - TTL-based caching with custom backends
- Interceptors - Request/response modification and logging
- File Handling - Streaming uploads/downloads with progress tracking
Examples
- Complete Examples - Full working examples with all features
Requirements
- PHP 8.1+
- Guzzle 7.0+
Optional Dependencies:
- PSR-18 HTTP Client:
psr/http-clientand a PSR-18 implementation (e.g.,symfony/http-client,nyholm/psr7) - XML Support: Built-in (uses PHP's SimpleXML)
- Symfony Serializer:
symfony/serializer^6.0 or ^7.0
Support
If you find this project helpful, consider supporting its development!
License
MIT
phpmystic/retrofit-php 适用场景与选型建议
phpmystic/retrofit-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「xml」 「http」 「rest」 「api」 「cache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpmystic/retrofit-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpmystic/retrofit-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpmystic/retrofit-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
Load DOM document safety
Bundle Symfony DaplosBundle
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
http客户端
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-24