# PHP package for managing HTTP requests

The HttpSoft\Request package implements Psr\Http\Message\RequestInterface and Psr\Http\Message\ServerRequestInterface in accordance with the RFC 7230 and RFC 7231 specifications.

This library provides HTTP requests functionality for interacting with the HTTP client and server application.

This package requires PHP version 7.4 or later.

Package installation:

composer require httpsoft/http-request

Source code on GitHub

# API

# Usage with HTTP Client

This library does not include a client implementation. To interact with the HTTP client, you need to create and fill in a Psr\Http\Message\RequestInterface instance, and the client must return a Psr\Http\Message\ResponseInterface instance.

use HttpSoft\Request\Request;
use HttpSoft\Request\RequestFactory;
use HttpSoft\Uri\UriFactory;

$method = 'POST';
$uri = 'http://example.com/auth';
$body = 'php://temp';
$headers = [
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ...',
];
$protocol = '1.1';

$request = new Request($method, $uri, $body, $headers, $protocol);
// equivalently to:
$request = RequestFactory::create($method, $uri, $body, $headers, $protocol);

// Using setters
$request = (new Request())
    ->withMethod($method)
    ->withUri(UriFactory::create($uri))
    ->withAddedHeader('Content-Type', 'application/json')
    ->withAddedHeader('Authorization', 'Bearer ...');

// Setting request target:
$uri = UriFactory::create('http://example.com');
// absolute-form
$request = $request->withRequestTarget((string) $uri);
// authority-form
$request = $request->withRequestTarget($uri->getAuthority());
// asterisk-form
$request = $request->withRequestTarget('*');

// Abstract example of sending a request to an HTTP client:
$request->getBody()->write(json_encode($data));
$response = $client->send($request);

Detailed description of using HttpSoft\Request\Request see here.

# Usage with Server-Side Application

To interact with the server-side application, you need to create and fill in a Psr\Http\Message\ServerRequestInterface instance, and in response it should return a Psr\Http\Message\ResponseInterface instance.

To create an instance of a HttpSoft\Request\ServerRequest, you must specify many data about the incoming request, which is stored in superglobal arrays.

use HttpSoft\Request\ServerRequest;

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

To avoid building all the data manually, you can use the HttpSoft\Request\ServerRequestFactory factory.

use HttpSoft\Request\ServerRequestFactory;

$request = ServerRequestFactory::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);
// equivalently to:
$request = ServerRequestFactory::createFromGlobals();
// equivalently to:
$request = ServerRequestFactory::create();

Detailed description of using HttpSoft\Request\ServerRequest see here.

Detailed description of using HttpSoft\Request\ServerRequestFactory see here.