hemiframe / php-http-message
PHP Classes implement PSR-7 Interfaces
Requires
- php: >=8.0
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^9.6 || ^10.5 || ^11.5 || ^12.0 || ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PSR-7 HTTP message implementation for PHP >= 8.0 (psr/http-message ^2.0).
Installation
composer require hemiframe/php-http-message
Classes
All classes are in the HemiFrame\Lib\Http\Message namespace: Message, Request, ServerRequest, Response, Uri, Stream and UploadedFile.
Usage
use HemiFrame\Lib\Http\Message\Response; use HemiFrame\Lib\Http\Message\ServerRequest; $request = (new ServerRequest())->fromGlobals(); $name = $request->getQueryParam('name', 'world'); $response = (new Response()) ->withStatus(200) ->withHeader('Content-Type', 'text/plain'); $response->getBody()->write('Hello '.$name); $response->send();
ServerRequest::fromGlobals() fills the request from $_SERVER, $_GET, $_COOKIE, $_POST and $_FILES. For JSON, multipart/form-data and application/x-www-form-urlencoded bodies of non-POST requests the body is parsed from the request input. See demo/index.php.
Immutability
Objects are immutable by default: every with*() method returns a modified copy. Call setImmutable(false) to modify the same instance instead:
$response = (new Response())->setImmutable(false); $response->withHeader('X-Test', '1'); // $response is changed
Development
composer install
composer run-script build # code style check + phpstan + phpunit
Client IP
ServerRequest::getClientIP() returns REMOTE_ADDR by default. Forwarding headers such as X-Forwarded-For can be spoofed by the client, so they are ignored unless you list the ones you trust, in priority order:
$request->setClientIpHeaders(['X-Forwarded-For']); $ip = $request->getClientIP();
Supported formats: plain lists (198.51.100.7, 10.0.0.1), addresses with a port (198.51.100.7:8080, [2001:db8::1]:443) and the RFC 7239 Forwarded header (for=198.51.100.7;proto=https). The first valid public IP is used; if none is found, the first valid IP is used, then REMOTE_ADDR. Passing [] restores the default.