znojil/http

ðŸŠķ Lightweight PSR-7, PSR-17 and PSR-18 implementation.

Maintainers

Details

github.com/znojil/http

Source

Issues

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/znojil/http

v1.0.0 2025-12-21 12:08 UTC

This package is auto-updated.

Last update: 2025-12-21 16:09:19 UTC


README

License PHP

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, ...}

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);

📄 License

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