PHPackages                             znojil/http - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. znojil/http

ActiveLibrary[HTTP &amp; Networking](/categories/http)

znojil/http
===========

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

v1.0.3(6mo ago)01191MITPHPPHP ^8.1CI passing

Since Dec 21Pushed 5mo agoCompare

[ Source](https://github.com/znojil/http)[ Packagist](https://packagist.org/packages/znojil/http)[ RSS](/packages/znojil-http/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (5)Versions (5)Used By (1)

Znojil HTTP
===========

[](#znojil-http)

[![Latest Stable Version](https://camo.githubusercontent.com/9e48c6132488b66dfecc38ae8497d3614d29ea2c29ee5e0cfd6aeb9aea7f6949/68747470733a2f2f706f7365722e707567782e6f72672f7a6e6f6a696c2f687474702f762f737461626c65)](https://github.com/znojil/http/releases)[![PHP Version Require](https://camo.githubusercontent.com/35499301b204260575669c837935b2316b1af634eddc43dbc22dfd8184249405/68747470733a2f2f706f7365722e707567782e6f72672f7a6e6f6a696c2f687474702f726571756972652f706870)](https://packagist.org/packages/znojil/http)[![License](https://camo.githubusercontent.com/a4ecd3e05cfb6eaf9a53e1706a39c38656f7752ccf2b98af95cdad1640b63e2a/68747470733a2f2f706f7365722e707567782e6f72672f7a6e6f6a696c2f687474702f6c6963656e7365)](LICENSE)[![Tests](https://github.com/znojil/http/actions/workflows/tests.yml/badge.svg)](https://github.com/znojil/http/actions)

**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
----------

[](#-features)

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

🚀 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require znojil/http
```

📖 Usage
-------

[](#-usage)

### 1. Sending a Request (Client)

[](#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

[](#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

[](#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)

[](#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)

[](#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)

[](#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
-----------------

[](#️-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
---------

[](#-license)

This library is open-source software licensed under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance70

Regular maintenance activity

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

4

Last Release

187d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6079815?v=4)[Marek Znojil](/maintainers/znojil)[@znojil](https://github.com/znojil)

---

Top Contributors

[![znojil](https://avatars.githubusercontent.com/u/6079815?v=4)](https://github.com/znojil "znojil (17 commits)")

---

Tags

curlhttp-clientphppsr-17psr-18psr-7httppsr-7clientpsr-17curlpsr-18lightweightznojil

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/znojil-http/health.svg)

```
[![Health](https://phpackages.com/badges/znojil-http/health.svg)](https://phpackages.com/packages/znojil-http)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B4.0k](/packages/guzzlehttp-psr7)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

185702.8k43](/packages/laudis-neo4j-php-client)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28150.5k](/packages/phpro-http-tools)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
