matraux/http-requests 问题修复 & 功能扩展

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

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

matraux/http-requests

最新稳定版本:1.0.2

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

Latest Version on Packagist Last release License: MIT PHP Security Policy Contributing QA Status Issues Last Commit


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

Examples

Single request

use Matraux\HttpRequests\Requester;
use Matraux\HttpRequests\Request\Method;
use Matraux\HttpRequests\Request\Request;

$requester = Requester::create();
$request = Request::create(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;
use Matraux\HttpRequests\Request\RequestCollection;

$requester = Requester::create();
$requests = RequestCollection::create();
$requests[] = Request::create(Method::Get, 'https://www.example.com/?page=1');
$requests[] = Request::create(Method::Get, 'https://www.example.com/?page=10');
$requests['custom'] = Request::create(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 = Requester::create();
$requester->onBefore[] = function() {
	echo "Sending request...\n";
};

$request = Request::create(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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-19