composer require initphp/httpOptional but recommended:
composer require --dev phpunit/phpunit "^9.6 || ^10.5"
composer require --dev phpstan/phpstan "^1.10"
composer require --dev http-interop/http-factory-tests "^1.1 || ^2.0"
composer require --dev php-http/psr7-integration-tests "^1.3"The split ^1.1 || ^2.0 constraint on http-interop/http-factory-tests lets Composer pick v1 on PHP 7.4 / 8.0 and v2 on PHP 8.1+ automatically; the v2 line requires PHP 8.1+ and PHPUnit 10.
<?php
require __DIR__ . '/vendor/autoload.php';
use InitPHP\HTTP\Client\Client;
use InitPHP\HTTP\Message\Request;
$client = new Client();
$response = $client->sendRequest(new Request('GET', 'https://httpbin.org/uuid'));
echo 'HTTP ' . $response->getStatusCode() . PHP_EOL;
echo (string) $response->getBody() . PHP_EOL;Three things just happened:
Requestconstructed a PSR-7 request value object.Client::sendRequest()shipped it over the wire via cURL.- The returned
Responseis a PSR-7 value object you can pass through middlewares, log, or transform.
It is the four PSR building blocks a PHP service needs to speak HTTP without manually writing cURL:
| Layer | Class / Facade | Purpose |
|---|---|---|
| PSR-7 | Message\{Request,Response,ServerRequest,Stream,Uri,UploadedFile} |
Immutable HTTP messages |
| PSR-17 | Factory\Factory |
A single factory that satisfies every PSR-17 sub-interface |
| PSR-18 | Client\Client |
cURL-backed PSR-18 transport |
| SAPI | Emitter\Emitter |
Push a Response to the running web server |
It isn't a router, a middleware dispatcher, or a request-handler pipeline (PSR-15). Those concerns live in higher-level packages — this library focuses on the message layer.
git clone https://github.com/InitPHP/HTTP.git
cd HTTP
composer install
composer ci # phpstan + phpunitThe suite includes the upstream php-http/psr7-integration-tests and http-interop/http-factory-tests compliance packs, plus an in-house PSR-18 smoke test against the PHP built-in server and a dedicated immutability suite.
- Read PSR-7 Messages for the immutability rules and the
with*()vsset*()distinction. - Read PSR-18 Configuration before shipping the client to production — the defaults are sane but not exhaustive.
- Skim the Recipes for one-page solutions to the common shapes.