Skip to content
Greg Bowler edited this page Apr 24, 2026 · 3 revisions

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 Request and ServerRequest objects
  • create and modify Response objects
  • work with Uri objects instead of string slicing
  • manage headers in a case-insensitive, iterable way
  • convert form and query-string data with browser-style helper objects
  • wrap $_SERVER metadata 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 RequestFactory to build the incoming request, passes Request and Response objects through the application lifecycle, exposes ResponseHeaders as 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.

Clone this wiki locally