-
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Greg Bowler edited this page Apr 24, 2026
·
2 revisions
This page collects a few small examples that show the shape of the library in practice.
use GT\Http\RequestFactory;
$request =new RequestFactory()
->createServerRequestFromGlobalState(
$_SERVER,
$_FILES,
$_GET,
$_POST
);
echo $request->getMethod();
echo $request->getUri()->getPath();use GT\Http\Response;
$response = new Response();
$response = $response
->withStatus(200)
->withHeader("Content-Type", "text/plain; charset=UTF-8");
$response->getBody()->write("Everything is working.");use GT\Http\Response;
$response = new Response();
$response->redirect("/login", 303);use GT\Http\Uri;
$uri = new Uri("https://example.com/search?q=phpgt&page=1");
$nextPage = $uri->withQueryValue("page", "2");
$withoutQuery = $uri->withQuery("");use GT\Http\Header\ResponseHeaders;
$headers = new ResponseHeaders();
$headers->add("Cache-Control", "no-store");
$headers->add("Set-Cookie", "theme=light; Path=/");
$headers->add("Set-Cookie", "session=abc123; HttpOnly; Path=/");
print_r($headers->getAll("Set-Cookie"));use GT\Http\FormData;
$formData = new FormData();
$formData->append("name", "Cody");
$formData->append("interest", "Mathematics");
$formData->append("interest", "Cryptography");
echo (string)$formData;use GT\Http\URLSearchParams;
$params = new URLSearchParams();
$params->append("page", "2");
$params->append("filter", "new");
$params->append("filter", "popular");
echo (string)$params;use GT\Http\ServerInfo;
$serverInfo = new ServerInfo($_SERVER);
echo $serverInfo->getRequestMethod();
echo $serverInfo->getFullUri();use GT\Http\ResponseStatusException\ClientError\HttpNotFound;
if(!$record) {
throw new HttpNotFound("The requested record does not exist.");
}use GT\Http\Response;
use GT\Http\ResponseFactory;
class HtmlResponse extends Response {}
class JsonResponse extends Response {}
ResponseFactory::registerResponseClass(HtmlResponse::class, "text/html");
ResponseFactory::registerResponseClass(JsonResponse::class, "application/json");
$response = ResponseFactory::create($request);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