httpsoft/http-server-request

Infrastructure for creating PSR-7 ServerRequest and UploadedFile

Maintainers

👁 devanych

Package info

github.com/httpsoft/http-server-request

Homepage

Documentation

pkg:composer/httpsoft/http-server-request

Statistics

Installs: 125 641

Dependents: 31

Suggesters: 0

Stars: 15

Open Issues: 0

1.1.1 2024-11-25 06:32 UTC

Requires

Requires (Dev)

Suggests

None

Conflicts

None

Replaces

None

MIT 1bf1b5de71920f0f7a49c17adf77963765bffd20

phphttppsr-7http-messagehttp-server-request

This package is auto-updated.

Last update: 2026-05-29 01:50:53 UTC


README

👁 License
👁 Latest Stable Version
👁 Total Downloads
👁 GitHub Build Status
👁 GitHub Static Analysis Status
👁 Scrutinizer Code Coverage
👁 Scrutinizer Code Quality

This package makes it easy and flexible to create PSR-7 components ServerRequest and UploadedFile.

Depends on the httpsoft/http-message package.

Documentation

Installation

This package requires PHP version 7.4 or later.

composer require httpsoft/http-server-request

Usage ServerRequestCreator

use HttpSoft\ServerRequest\ServerRequestCreator;

// All necessary data will be received automatically:
$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);
// equivalently to:
$request = ServerRequestCreator::createFromGlobals();
// equivalently to:
$request = ServerRequestCreator::create();

By default HttpSoft\ServerRequest\SapiNormalizer is used for normalization of server parameters. You can use your own server parameters normalizer, for this you need to implement an HttpSoft\ServerRequest\ServerNormalizerInterface interface.

$normalizer = new YouCustomServerNormalizer();

$request = ServerRequestCreator::create($normalizer);
// equivalently to:
$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer);
// or with custom superglobals:
$request = ServerRequestCreator::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer);

Usage UploadedFileCreator

use HttpSoft\ServerRequest\UploadedFileCreator;

/** @var StreamInterface|string|resource $streamOrFile */
$uploadedFile = UploadedFileCreator::create($streamOrFile, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');

// Create a new `HttpSoft\UploadedFile\UploadedFile` instance from array (the item `$_FILES`)
$uploadedFile = UploadedFileCreator::createFromArray([
 'name' => 'filename.jpg', // optional
 'type' => 'image/jpeg', // optional
 'tmp_name' => '/tmp/php/php6hst32',
 'error' => 0, // UPLOAD_ERR_OK
 'size' => 98174,
]);

// Normalizes the superglobal structure and converts each array
// value to an instance of `Psr\Http\Message\UploadedFileInterface`.
$uploadedFiles = UploadedFileCreator::createFromGlobals($_FILES);