定制 shoxcie/batch-http-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shoxcie/batch-http-client

Composer 安装命令:

composer require shoxcie/batch-http-client

包简介

HTTP request batch executor with individual retries, built on Symfony HttpClient.

README 文档

README

HTTP request batch executor with individual retries, built on Symfony HttpClient.

Fire multiple HTTP requests in parallel. Each request retries independently without blocking others.

Requirements

  • PHP 8.4+
  • Symfony HttpClient 8.0+

Installation

composer require shoxcie/batch-http-client

Usage

use Shoxcie\BatchHttpClient\BatchHttpClient;
use Shoxcie\BatchHttpClient\RequestConfig;

$client = new BatchHttpClient();

$results = $client
    ->request([
        'users' => new RequestConfig('GET', 'https://api.example.com/users'),
        'orders' => new RequestConfig('POST', 'https://api.example.com/orders', options: [
            'json' => ['item' => 'widget', 'qty' => 3],
        ]),
    ])
    ->fetch();

$results['users'];  // decoded JSON array
$results['orders']; // decoded JSON array

Retries

$results = $client
    ->request([
        'flaky' => new RequestConfig('GET', 'https://api.example.com/flaky',
            maxRetries: 3,
        ),
    ])
    ->fetch();

Any non-2xx response or transport error (connection timeout, DNS failure) triggers a retry. Transport exception retries can be disabled per request with retryOnTransportException: false.

Retry options

Override options on retry with a static array:

new RequestConfig('GET', 'https://api.example.com/resource',
    maxRetries: 2,
    retryOptions: ['timeout' => 30],
)

Or dynamically with a Closure:

new RequestConfig('GET', 'https://api.example.com/resource',
    maxRetries: 3,
    retryOptions: function (string $key, int $retries, ExceptionInterface|InvalidResponseException $e): array {
        return ['timeout' => 10 * $retries];
    },
)

Retry options are merged onto the original options via array_replace_recursive().

Callbacks

$results = $client
    ->request([...])
    ->onSuccess(function (string $key, int $retries, mixed $result, ResponseInterface $response) {
        // called for each 2xx response, after parseResponse if configured
        // $retries = 0 on first-attempt success, N on success after N retries
    })
    ->onRetry(function (string $key, int $retries, ResponseInterface $failedResponse, ExceptionInterface|InvalidResponseException $e, ResponseInterface $retryResponse) {
        // called when a retry fires; $retries is the retry count after this retry fired (always >= 1)
    })
    ->onExhausted(function (string $key, int $retries, ResponseInterface $response, ExceptionInterface|InvalidResponseException $e) {
        // called when a single request exhausts all retries
        // $retries equals maxRetries normally; less when a transport error short-circuits
    })
    ->onAbort(function (string $key, int $retries, ResponseInterface $response, Throwable $e) {
        // called when an unexpected exception (e.g. throwing user callback) cancels the whole batch
        // $retries is the retry count for the request being processed when the abort fired
    })
    ->fetch();

Error handling

By default, if a request exhausts all retries, the last exception is rethrown and all in-flight requests are cancelled:

new RequestConfig('GET', 'https://api.example.com/critical',
    maxRetries: 3,
    throwOnExhausted: true, // default
)

Set throwOnExhausted: false for optional requests. Failed optional requests return null in the results array:

new RequestConfig('GET', 'https://api.example.com/optional',
    throwOnExhausted: false,
)

Response decoding

By default, responses are decoded as JSON (toArray()). Set decodeJson: false to get raw content (getContent()):

new RequestConfig('GET', 'https://example.com/file.csv',
    decodeJson: false,
)

Response parsing / validation

parseResponse runs after the body is decoded and before onSuccess, only on a 2xx response. The return value replaces the entry in the results array, so it doubles as a custom parser:

new RequestConfig('GET', 'https://api.example.com/users',
    parseResponse: fn(string $key, mixed $result, ResponseInterface $response): mixed
        => $result['data'],
)

Throw InvalidResponseException from the parser to reject a semantically invalid 2xx response and trigger a retry (counts against maxRetries, fires onRetry, and on exhaustion fires onExhausted plus rethrows if throwOnExhausted: true):

use Shoxcie\BatchHttpClient\InvalidResponseException;

new RequestConfig('GET', 'https://api.example.com/job-status',
    maxRetries: 5,
    parseResponse: function (string $key, mixed $result): mixed {
        if ($result['status'] === 'pending') {
            throw new InvalidResponseException('job not finished');
        }

        return $result;
    },
)

Any other Throwable from the parser is treated as an unexpected error and routes to onAbort, cancelling the whole batch.

Custom HTTP client

Pass any HttpClientInterface implementation:

use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create([
    'timeout' => 10,
    'max_duration' => 30,
]);

$client = new BatchHttpClient($httpClient);

RequestConfig reference

Parameter Type Default Description
method string (required) HTTP method
url string (required) Request URL
options array [] Symfony HttpClient options
retryOptions array|Closure [] Options merged on retry, or Closure receiving (string $key, int $retries, ExceptionInterface|InvalidResponseException $e)
throwOnExhausted bool true Rethrow exception after retries exhausted
decodeJson bool true Decode response as JSON
maxRetries int 0 Maximum retry attempts
retryOnTransportException bool true Retry on transport errors (timeouts, DNS)
parseResponse Closure|null null Runs on 2xx responses before onSuccess. Receives (string $key, int $retries, mixed $result, ResponseInterface $response); return value replaces the result. Throw InvalidResponseException to retry.

Important

The user_data option is reserved for internal key correlation — passing it in options or retryOptions throws InvalidArgumentException.

Upgrading

See the upgrade/ folder for migration guides between major versions.

License

MIT — see LICENSE for details.

shoxcie/batch-http-client 适用场景与选型建议

shoxcie/batch-http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 shoxcie/batch-http-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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