# Class HttpSoft\Message\ServerRequest

Class, that implements Psr\Http\Message\ServerRequestInterface.

The HttpSoft\Message\ServerRequest object are immutable. You can use setters (with the with and without prefix) that return a new instance of the class with the changed data.

Source code on GitHub.

use HttpSoft\Message\ServerRequest;

$request = new ServerRequest(
    $_SERVER,
    $_FILES,
    $_COOKIE,
    $_GET,
    $_POST,
    'GET', // Request method
    'http://example.com', // Request URI
    ['Host' => 'example.com', /* Other headers */],
    'php://temp', // HTTP message body
    '1.1' // HTTP protocol version
);

// Using setters
$request
    ->withUploadedFiles([/* Files data */])
    ->withCookieParams([/* Cookie data */])
    ->withQueryParams([/* Get data */])
    ->withParsedBody([/* Post data */])
    ->withAttribute('page', '3' /* Any custom data */);

# Public methods

See the original detailed description of the methods in the Psr\Http\Message\ServerRequestInterface.

/**
 * @param array $serverParams
 * @param array $uploadedFiles
 * @param array $cookieParams
 * @param array $queryParams
 * @param array|object|null $parsedBody
 * @param string $method
 * @param UriInterface|string $uri
 * @param array $headers
 * @param StreamInterface|string|resource $body
 * @param string $protocol
 */
public function __construct(
    array $serverParams = [],
    array $uploadedFiles = [],
    array $cookieParams = [],
    array $queryParams = [],
    $parsedBody = null,
    string $method = 'GET',
    $uri = '',
    array $headers = [],
    $body = 'php://temp',
    string $protocol = '1.1'
);

The methods that are defined in the Psr\Http\Message\ServerRequestInterface interface, are shown below. All other methods with descriptions are in the used traits (HttpSoft\Message\RequestTrait and HttpSoft\Message\MessageTrait).

# getServerParams

Returns server parameters.

public function getServerParams(): array;

Returns cookies.

public function getCookieParams(): array;

Return a new instance with the specified cookies.

public function withCookieParams(array $cookies): ServerRequestInterface;

The $cookies array must be compatible with the structure of $_COOKIE.

$request->getCookieParams(); // []

$newRequest = $request->withCookieParams(['cookie_name' => 'cookie-value']);
$newRequest->getCookieParams(); // ['cookie_name' => 'cookie-value']

$request->getCookieParams(); // []

# getQueryParams

Returns query string arguments.

public function getQueryParams(): array;

# withQueryParams

Returns a new instance with the specified query string arguments.

public function withQueryParams(array $query): ServerRequestInterface;

The $query array must be compatible with the structure of $_GET.

$request->getQueryParams(); // []

$newRequest = $request->withQueryParams(['query_string_key' => 'query-string-value']);
$newRequest->getQueryParams(); // ['query_string_key' => 'query-string-value']

$request->getQueryParams(); // []

# getUploadedFiles

Returns normalized file uploaded data.

public function getUploadedFiles(): array;

This method returns upload metadata in a normalized tree, with each item an instance of Psr\Http\Message\UploadedFileInterface.

# withUploadedFiles

Returns a new instance with the specified uploaded files.

public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface;

The $uploadedFiles array must be a normalized tree, with each item an instance of Psr\Http\Message\UploadedFileInterface, otherwise the \InvalidArgumentException exception will be thrown.

$request->getUploadedFiles(); // []

$newRequest = $request->withUploadedFiles([
    'file' => new UploadedFile('/tmp/phpN3FmFr', 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain'),
]);
$newRequest->getUploadedFiles()['file']->getClientFilename(); // 'file.txt'
$newRequest->getUploadedFiles()['file']->getClientMediaType(); // 'text/plain'
$newRequest->getUploadedFiles()['file']->getStream()->getMetadata('uri'); // '/tmp/phpN3FmFr'
$newRequest->getUploadedFiles()['file']->getError(); // 0 (UPLOAD_ERR_OK)
$newRequest->getUploadedFiles()['file']->getSize(); // 1024

$request->getUploadedFiles(); // []

// throws InvalidArgumentException
$request->withUploadedFiles([
    'file' => [
        'name' => 'file.txt',
        'type' => 'text/plain',
        'tmp_name' => '/tmp/phpN3FmFr',
        'error' => UPLOAD_ERR_OK,
        'size' => 1024,
    ],
]);

# getParsedBody

Returns parameters provided in the request body.

/**
 * @return null|array|object
 */
public function getParsedBody();

# withParsedBody

Returns a new instance with the specified request body parameters.

/**
 * @param null|array|object $data
 * @return static
 */
public function withParsedBody($data): ServerRequestInterface;

If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, $data value must be the contents of $_POST. Otherwise, any results of deserializing the contents of the request body can be returned, but only as an array or object, or null, else the \InvalidArgumentException exception will be thrown.

$request->getParsedBody(); // []

$newRequest = $request->withParsedBody(['post_data_key' => 'post_data-value']);
$newRequest->getParsedBody(); // ['post_data_key' => 'post_data-value']

$request->getParsedBody(); // []

// throws InvalidArgumentException
$request->withParsedBody(json_encode(['post_data_key' => 'post_data-value']);

# getAttributes

Returns all attributes derived from the request if has been previously set.

public function getAttributes(): array;

The request «attributes» may be used to allow injection of any parameters derived from the request: e.g., the results of path match operations; the results of decrypting cookies; the results of deserializing non-form-encoded message bodies; etc. Attributes will be application and request specific, and can be mutable.

# getAttribute

Returns single attribute derived from the request or default value if the attribute has not been previously set.

/**
 * @see getAttributes()
 * @param string $name
 * @param mixed $default
 * @return mixed
 */
public function getAttribute($name, $default = null);

# withAttribute

Returns a new instance with the specified derived request attribute.

/**
 * @see getAttributes()
 * @param string $name
 * @param mixed $value
 * @return static
 */
public function withAttribute($name, $value): ServerRequestInterface;

This method allows setting a single derived request attribute as described in getAttributes().

$request->getAttributes(); // []
$request->getAttribute('page'); // null
$request->getAttribute('page', 0); // 0

$newRequest = $request->withAttribute('page', 3);
$newRequest->getAttributes(); // ['page' => 3]
$newRequest->getAttribute('page'); // 3

$request->getAttributes(); // []

# withoutAttribute

Returns a new instance without the specified derived request attribute.

/**
 * @see getAttributes()
 * @param string $name
 * @return static
 */
public function withoutAttribute($name): ServerRequestInterface;

This method allows removing a single derived request attribute as described in getAttributes().

$request->getAttributes(); // ['page' => 3]
$request->getAttribute('page'); // 3

$newRequest = $request->withoutAttribute('page');
$newRequest->getAttributes(); // []
$newRequest->getAttribute('page'); // null
$newRequest->getAttribute('page', 0); // 0

$request->getAttributes(); // ['page' => 3]