-
-
Notifications
You must be signed in to change notification settings - Fork 0
Overview
HTTP can feel abstract at first because there are a lot of moving parts: methods, URIs, headers, bodies, status codes, and server metadata. This package does not invent any of those ideas, it wraps them in PHP classes so we can work with them directly.
The centre of the package is the PSR-7 message layer:
GT\Http\RequestGT\Http\ServerRequestGT\Http\ResponseGT\Http\StreamGT\Http\Uri
These are the objects you will use most often.
Around the PSR-7 types there are several supporting classes:
-
GT\Http\RequestFactorybuilds aServerRequestfrom PHP globals. -
GT\Http\ResponseFactorypicks a response class from the request'sAcceptheader. -
GT\Http\ServerInfowraps raw$_SERVERdata. -
GT\Http\Header\Headers,HeaderLine, andParserhelp with header handling. -
GT\Http\FormDataandGT\Http\URLSearchParamsmirror familiar browser APIs. -
GT\Http\Blob,File, andArrayBufferprovide binary and body-data helpers. -
GT\Http\UriResolverresolves relative references against a base URI.
The ResponseStatusException namespaces contain exception classes such as:
GT\Http\ResponseStatusException\ClientError\HttpNotFoundGT\Http\ResponseStatusException\ClientError\HttpUnauthorizedGT\Http\ResponseStatusException\ServerError\HttpInternalServerError
These are useful when application flow should stop with a specific HTTP status.
In a typical server-side request:
- PHP gives us raw arrays such as
$_SERVER,$_GET,$_POST, and$_FILES. -
RequestFactoryturns those into aServerRequest. - Application code reads from the request, URI, headers, and body.
- Application code creates or updates a
Response. - The response headers and stream body are sent back to the browser.
use GT\Http\RequestFactory;
use GT\Http\Response;
$request = new RequestFactory()
->createServerRequestFromGlobalState(
$_SERVER,
$_FILES,
$_GET,
$_POST
);
$response = new Response();
$response = $response->withHeader("Content-Type", "text/plain");
$response->getBody()->write("You requested " . $request->getUri()->getPath());The aim of this library is to deliberately resemble browser APIs as closely as possible.
For example:
-
FormDatacan represent form body data and can also be constructed from a DOM<form>element. -
URLSearchParamshandles query-string style key/value pairs. -
BlobandArrayBuffermirror common web platform objects.
This makes the package especially comfortable to use alongside other PHP.GT components, because the mental model stays close to the web platform.
Tip
PHP.GT/Fetch uses the classes within this library to implement the Fetch HTTP client in PHP. For example: $http->fetch("https://example.com", ["body" => new FormData()]);
If you are starting from scratch, focus on these pages in this order:
- Creating requests from global state
- Request and ServerRequest objects
- Response objects
- URI objects
- Working with headers
Once those are familiar, the rest of the helper classes will make much more sense.
Next, we will look at the most common entry point on the server: Creating requests from global state.
PHP.GT/Http is a separately maintained component of PHP.GT/WebEngine.
- Creating requests from global state
- Request and ServerRequest objects
- Response objects
- URI objects
- Working with headers
- Parts of the HTTP header