Http documentation
Encapsulated HTTP messages and helpers for PHP.
phpgt/http provides the low-level HTTP objects used throughout the PHP.GT ecosystem. It implements the core PSR-7 message interfaces, but it also adds a set of practical helpers around URIs, headers, form bodies, query strings, streams, and HTTP status handling.
If you are new to HTTP libraries in PHP, the important idea is simple: this package gives us PHP objects that represent the same pieces of web traffic the browser and the server already exchange all day long.
That means we can:
- build and inspect
RequestandServerRequestobjects - create and modify
Responseobjects - work with
Uriobjects instead of string slicing - manage headers in a case-insensitive, iterable way
- convert form and query-string data with browser-style helper objects
- wrap
$_SERVERmetadata in a predictable API - represent common HTTP errors as dedicated exception classes
Install the package
composer require phpgt/http
If you are working inside WebEngine, this package is already part of the normal request/response lifecycle.
[!NOTE] If you are building with WebEngine, you will not usually construct every object here by hand. WebEngine uses
RequestFactoryto build the incoming request, passesRequestandResponseobjects through the application lifecycle, exposesResponseHeadersas a service, and relies on the HTTP status exception classes when generating error responses.
A small example
use GT\Http\RequestFactory;
use GT\Http\Response;
$request = (new RequestFactory())
->createServerRequestFromGlobalState(
$_SERVER,
$_FILES,
$_GET,
$_POST
);
$response = new Response();
$response = $response
->withStatus(200)
->withHeader("Content-Type", "text/plain");
$response->getBody()->write("Hello, " . $request->getMethod());
In practice, that means the incoming HTTP request is wrapped in an object you can pass around safely, and the outgoing response is built in the same way.
To see how the pieces fit together before diving into the individual classes, continue to the Overview.