matraux/http-requests
最新稳定版本:1.1.0
Composer 安装命令:
composer require matraux/http-requests
包简介
A PHP 8.4+ library for creating and batching HTTP requests with type‑safe wrappers, event hooks, and ergonomic collections, powered by Guzzle and PSR‑7.
README 文档
README
Introduction
A PHP 8.4+ library for working with HTTP requests and responses in a structured, type‑safe way. Provides easy creation of single or batch HTTP requests, ergonomic collections, and seamless integration with PSR‑7 and Guzzle. Includes built‑in event hooks (onBefore, onAfter, onSuccess, onFail) for logging, debugging, authentication, or custom processing at each stage of the request lifecycle. Errors from the transport layer are normalized into PSR‑7 responses (no exceptions are thrown from send()/sendBatch()), making the flow predictable in both single and parallel/batch scenarios.
Features
- Object‑oriented HTTP request/response wrappers (PSR‑7 compatible); Response also keeps the original Request
- Type‑safe API: Method enum or string, Stringable URI, strict containers for headers, config, and collections
- Ergonomic collections: RequestCollection (mutable) and ResponseCollection (read‑only)
- Easy batching and parallel execution via Guzzle promises (sendAsync) and Utils::settle()
- Built‑in events: onBefore, onAfter, onSuccess(ResponseInterface), onFail(GuzzleException) for hooks, logging, authentication, and custom logic
- Default headers merging: headers set on Requester are applied to every Request right before sending
- Unified error handling: exceptions are mapped to a PSR‑7 Response; when no response is available, a synthetic 500 is created with the exception message
- Simple API for reading status, headers, and body (getStatusCode(), getHeaderLine(), getBody())
- Lazy Guzzle client configured through a typed Config container
- Designed for easy testing and debugging; event handler failures are isolated
Installation
composer require matraux/http-requests
Requirements
| version | PHP | Note |
|---|---|---|
| 1.0.0 | 8.4+ | Initial release |
| 1.0.1 | 8.4+ | Switch to Codeception |
| 1.0.2 | 8.4+ | Lazy access request options |
| 1.1.0 | 8.4+ | Code optimization, removed dependencies |
Examples
Single request
use Matraux\HttpRequests\Requester; use Matraux\HttpRequests\Request\Method; use Matraux\HttpRequests\Request\Request; $requester = new Requester; $request = new Request(Method::Get, 'https://www.example.com'); $response = $requester->send($request); echo $response->getStatusCode(); // e.g. 200, 404, 500, ... echo $response->getBody()->getContents(); // Response body echo $response->getHeaderLine('Content-Type'); // e.g. text/html, application/json
Batch requests (collections)
use Matraux\HttpRequests\Requester; use Matraux\HttpRequests\Request\Method; use Matraux\HttpRequests\Request\Request; $requester = new Requester; $requests = []; $requests[] = new Request(Method::Get, 'https://www.example.com/?page=1'); $requests[] = new Request(Method::Get, 'https://www.example.com/?page=10'); $requests['custom'] = new Request(Method::Get, 'https://www.example.com/?page=100'); $responses = $requester->sendBatch($requests); foreach ($responses as $index => $response) { echo "$index: " . $response->getStatusCode() . "\n"; } echo $responses['custom']->getBody()->getContents();
Using events
use Matraux\HttpRequests\Requester; use Matraux\HttpRequests\Request\Method; use Matraux\HttpRequests\Request\Request; use Psr\Http\Message\ResponseInterface $requester = new Requester(); $requester->onBefore[] = function() { echo "Sending request...\n"; }; $request = new Request(Method::Get, 'https://www.example.com'); $request->onBefore[] = function() use ($request) { echo "Requesting URL: " . $request->uri . "\n"; }; $request->onSuccess[] = function(ResponseInterface $psrResponse): void { echo "Success response: " . $psrResponse->getStatusCode() . "\n"; }; $response = $requester->send($request);
Development
See Development for debug, test instructions, static analysis, and coding standards.
Support
For bug reports and feature requests, please use the issue tracker.
matraux/http-requests 适用场景与选型建议
matraux/http-requests 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「Guzzle」 「HTTP requests」 「MATRAUX」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 matraux/http-requests 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 matraux/http-requests 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 matraux/http-requests 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Rest client to make GET, POST, PUT, DELETE, PATCH, etc calls.
This package is free and can be used for API Brazil website functions
A simple package to better handle application requests.
Cloudflare Middleware For Guzzle
Simplifies working with holaluz API endpoints
统计信息
- 总下载量: 143
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-19