phpmystic/retrofit-php 问题修复 & 功能扩展

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

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

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

Advanced Features

Examples

Requirements

  • PHP 8.1+
  • Guzzle 7.0+

Optional Dependencies:

  • PSR-18 HTTP Client: psr/http-client and 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!

Buy Me A Coffee

Buy Me A Coffee

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpmystic/retrofit-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-24