tcds-io/php-jackson-guzzle 问题修复 & 功能扩展

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

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

tcds-io/php-jackson-guzzle

Composer 安装命令:

composer require tcds-io/php-jackson-guzzle

包简介

A Guzzle plugin that parses request objects into json and response into objects

README 文档

README

PHP Tests

Guzzle integration for tcds-io/php-jackson, a type-safe object mapper inspired by Jackson (Java).

This package provides a typed HTTP client built on top of Guzzle that automatically maps request DTOs into Guzzle options and JSON responses into strongly typed PHP objects, synchronously and asynchronously.

Features

  • Typed object mapping for HTTP responses
  • Request DTO mapping for query params, JSON bodies, and form params
  • Full async support with typed promises
  • Works with immutable and readonly DTOs
  • Keeps the native Guzzle client accessible
  • Built on top of php-jackson for consistent serialization rules

Installation

composer require tcds-io/php-jackson-guzzle

How it works

  1. Create a JacksonClient with a native Guzzle client.
  2. Pass the expected response DTO class to get, post, put, patch, or request.
  3. Optionally pass request DTOs as query params, JSON body, or form params.
  4. Guzzle sends the HTTP request.
  5. PHP-Jackson deserializes the JSON response into your expected DTO.

Basic usage

use GuzzleHttp\Client;
use Tcds\Io\Jackson\Guzzle\JacksonClient;

$client = new JacksonClient(
    new Client([
        'base_uri' => 'https://api.example.com',
    ]),
);

Typed GET

readonly class Address
{
    public function __construct(
        public string $id,
        public string $street,
        public int $number,
        public bool $main,
    ) {}
}

$address = $client->get(Address::class, '/addresses/aaa');

Typed POST with a JSON body

readonly class CreateAddress
{
    public function __construct(
        public string $street,
        public int $number,
    ) {}
}

readonly class AddressCreated
{
    public function __construct(public string $id) {}
}

$created = $client->post(
    class: AddressCreated::class,
    uri: '/addresses',
    jsonBody: new CreateAddress(street: 'Ocean Avenue', number: 42),
);

You can still pass native Guzzle options when needed:

use GuzzleHttp\RequestOptions;

$created = $client->post(
    class: AddressCreated::class,
    uri: '/addresses',
    options: [
        RequestOptions::HEADERS => ['X-Request-ID' => 'abc-123'],
    ],
    jsonBody: new CreateAddress(street: 'Ocean Avenue', number: 42),
);

If an option is already present in options, the client leaves it untouched.

Request data

JacksonClient can serialize DTOs into the common Guzzle request option buckets:

$response = $client->request(
    class: AddressCreated::class,
    method: 'POST',
    uri: '/addresses',
    queryParams: new SearchAddress(street: 'Ocean Avenue'),
    jsonBody: new CreateAddress(street: 'Ocean Avenue', number: 42),
    formParams: new AuditFields(source: 'backoffice'),
);
  • queryParams maps to RequestOptions::QUERY
  • jsonBody maps to RequestOptions::JSON
  • formParams maps to RequestOptions::FORM_PARAMS

Pass null to omit an option. Empty arrays are treated as intentional payloads and are still passed to Guzzle.

Async

$address = $client
    ->getAsync(Address::class, '/addresses/aaa')
    ->wait();

The async methods return JacksonPromise, which decorates Guzzle's promise and maps fulfilled ResponseInterface values into your expected DTO.

$client
    ->postAsync(
        class: AddressCreated::class,
        uri: '/addresses',
        jsonBody: new CreateAddress(street: 'Ocean Avenue', number: 42),
    )
    ->then(fn(AddressCreated $created) => $created->id)
    ->wait();

Custom mappers

Pass php-jackson type mappers to the JacksonClient constructor when a type needs custom read or write behavior.

use GuzzleHttp\Client;
use Tcds\Io\Jackson\Guzzle\JacksonClient;

$client = new JacksonClient(
    guzzle: new Client([
        'base_uri' => 'https://api.example.com',
    ]),
    typeMappers: [
        User::class => [
            'reader' => fn(array $data) => new User(
                name: $data['name'],
                lastName: $data['surname'],
            ),
            'writer' => fn(User $user) => [
                'name' => $user->name,
                'surname' => $user->lastName,
            ],
        ],
    ],
);

Please refer to the core mapper documentation for additional configuration options.

Development

composer install
vendor/bin/phpunit --testdox

Related packages

tcds-io/php-jackson-guzzle 适用场景与选型建议

tcds-io/php-jackson-guzzle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.09k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tcds-io/php-jackson-guzzle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tcds-io/php-jackson-guzzle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-13