定制 znojil/http 二次开发

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

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

znojil/http

Composer 安装命令:

composer require znojil/http

包简介

🪶 Lightweight PSR-7, PSR-17 and PSR-18 implementation.

README 文档

README

Latest Stable Version PHP Version Require License Tests

Lightweight, strict, and robust implementation of PSR-7, PSR-17, and PSR-18 standards.

This library provides a clean HTTP Client (wrapper around cURL) and a complete set of HTTP Message objects (Request, Response, Stream, URI, etc.) strictly following PHP standards. It is designed to be lightweight with zero unnecessary dependencies.

📦 Features

  • PSR-7 Implementation (HTTP Message interfaces)
  • PSR-17 Implementation (HTTP Factories)
  • PSR-18 Implementation (HTTP Client)
  • Zero dependencies (production)
  • Strict types & PHPStan Level Max compatible
  • Immutable objects design

🚀 Installation

Install via Composer:

composer require znojil/http

📖 Usage

1. Sending a Request (Client)

The Client automatically detects the provided ResponseFactory (PSR-17). If none is provided, it uses the internal implementation.

use Znojil\Http\Client;
use Znojil\Http\RequestFactory;

$client = new Client;
$factory = new RequestFactory;

// Create a PSR-7 Request
$request = $factory->postJson('https://api.example.com/users', [
	'name' => 'John Doe',
	'role' => 'admin'
]);

// Send Request (PSR-18)
$response = $client->sendRequest($request);

echo $response->getStatusCode(); // 201
echo (string) $response->getBody(); // {"id": 1, ...}

Client Configuration

The Client constructor accepts optional parameters for configuring base URI, default headers, and cURL options:

use Znojil\Http\Client;

$client = new Client(
	'https://api.example.com/v1',
	[
		'Authorization' => 'Bearer token123',
		'Accept' => 'application/json'
	],
	[
		CURLOPT_TIMEOUT => 30,
		CURLOPT_CONNECTTIMEOUT => 5
	]
);

// Now you can use relative URLs - base URI will be automatically combined
$response = $client->sendRequest($factory->get('/users?limit=10'));
// Requests: https://api.example.com/v1/users?limit=10

RequestFactory Helper Methods

The RequestFactory provides convenient methods for common HTTP operations:

use Znojil\Http\RequestFactory;

$factory = new RequestFactory;

// GET request with query parameters
$request = $factory->get('https://api.example.com/users', ['page' => 1, 'limit' => 10]);

// POST with JSON body
$request = $factory->postJson('https://api.example.com/users', ['name' => 'John']);

// POST with form data
$request = $factory->post('https://api.example.com/login', [
	'username' => 'john',
	'password' => 'secret'
], ['Content-Type' => 'application/x-www-form-urlencoded']);

// PUT, PATCH, DELETE
$request = $factory->putJson('https://api.example.com/users/1', ['name' => 'Jane']);
$request = $factory->patchJson('https://api.example.com/users/1', ['status' => 'active']);
$request = $factory->delete('https://api.example.com/users/1');

File Upload (Multipart)

For file uploads with additional form fields, use cURL's native CURLFile via CURLOPT_POSTFIELDS:

use Znojil\Http\Client;
use Znojil\Http\RequestFactory;

$client = new Client;
$factory = new RequestFactory;

$request = $factory->post('https://api.example.com/upload');

$response = $client->sendRequest($request, [
	CURLOPT_POSTFIELDS => [
		'title' => 'My Document',
		'category' => 'reports',
		'file' => new \CURLFile('/path/to/document.pdf', 'application/pdf', 'document.pdf')
	]
]);

2. Handling Incoming Request (Server)

Ideal for API endpoints or webhook processing.

use Znojil\Http\Message\ServerRequest;

// Create request from PHP globals ($_GET, $_POST, $_FILES...)
$request = ServerRequest::fromGlobals();

$method = $request->getMethod();
$queryParams = $request->getQueryParams();
$body = $request->getParsedBody();

// Working with Uploaded Files
$files = $request->getUploadedFiles();
if(isset($files['document']) && $files['document']->isOk()){
	$files['document']->moveTo('/storage/uploads/doc.pdf');
}

3. Using Factories (PSR-17)

You can use the factories to create any PSR-7 object manually.

use Znojil\Http\Psr17Factory;

$factory = new Psr17Factory;

$uri = $factory->createUri('https://example.com');
$stream = $factory->createStream('Hello World');
$response = $factory->createResponse(200)->withBody($stream);

⚠️ Error Handling

The library throws PSR-18 compliant exceptions:

use Znojil\Http\Client;
use Znojil\Http\Exception\NetworkException;
use Znojil\Http\Exception\ClientException;

$client = new Client;

try{
	$response = $client->sendRequest($request);
}catch(NetworkException $e){
	// Network-level errors (DNS failure, connection timeout, etc.)
	echo 'Network error: ' . $e->getMessage();
	$failedRequest = $e->getRequest(); // Access the original request
}catch(ClientException $e){
	// Client initialization errors
	echo 'Client error: ' . $e->getMessage();
}

📄 License

This library is open-source software licensed under the MIT license.

znojil/http 适用场景与选型建议

znojil/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 106 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「curl」 「http」 「client」 「lightweight」 「psr-7」 「psr-17」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-21