Skip to content

Overview

Greg Bowler edited this page Apr 24, 2026 · 2 revisions

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 main object groups

Messages

The centre of the package is the PSR-7 message layer:

  • GT\Http\Request
  • GT\Http\ServerRequest
  • GT\Http\Response
  • GT\Http\Stream
  • GT\Http\Uri

These are the objects you will use most often.

Supporting helpers

Around the PSR-7 types there are several supporting classes:

  • GT\Http\RequestFactory builds a ServerRequest from PHP globals.
  • GT\Http\ResponseFactory picks a response class from the request's Accept header.
  • GT\Http\ServerInfo wraps raw $_SERVER data.
  • GT\Http\Header\Headers, HeaderLine, and Parser help with header handling.
  • GT\Http\FormData and GT\Http\URLSearchParams mirror familiar browser APIs.
  • GT\Http\Blob, File, and ArrayBuffer provide binary and body-data helpers.
  • GT\Http\UriResolver resolves relative references against a base URI.

Status exceptions

The ResponseStatusException namespaces contain exception classes such as:

  • GT\Http\ResponseStatusException\ClientError\HttpNotFound
  • GT\Http\ResponseStatusException\ClientError\HttpUnauthorized
  • GT\Http\ResponseStatusException\ServerError\HttpInternalServerError

These are useful when application flow should stop with a specific HTTP status.

Typical usage

In a typical server-side request:

  1. PHP gives us raw arrays such as $_SERVER, $_GET, $_POST, and $_FILES.
  2. RequestFactory turns those into a ServerRequest.
  3. Application code reads from the request, URI, headers, and body.
  4. Application code creates or updates a Response.
  5. 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());

Web-style helper objects

The aim of this library is to deliberately resemble browser APIs as closely as possible.

For example:

  • FormData can represent form body data and can also be constructed from a DOM <form> element.
  • URLSearchParams handles query-string style key/value pairs.
  • Blob and ArrayBuffer mirror 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()]);

What to learn first

If you are starting from scratch, focus on these pages in this order:

  1. Creating requests from global state
  2. Request and ServerRequest objects
  3. Response objects
  4. URI objects
  5. 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.

Clone this wiki locally