Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 3.68 KB

README.md

File metadata and controls

73 lines (54 loc) · 3.68 KB

HTTP Server Request

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