FrankenPHP\Request
(True Async 0.6+)
The Request object is passed to your handler callback by HttpServer::onRequest(). It provides read-only access to all HTTP request data.
Each request coroutine receives its own Request instance — there are no shared globals, so concurrent handlers are always safe.
Class Synopsis
namespace FrankenPHP;
class Request
{
public function getMethod(): string;
public function getUri(): string;
public function getHeader(string $name): ?string;
public function getHeaders(): array;
public function getBody(): string;
public function getQueryParams(): array;
public function getCookies(): array;
public function getHost(): string;
public function getRemoteAddr(): string;
public function getScheme(): string;
public function getProtocolVersion(): string;
public function getParsedBody(): array;
public function getUploadedFiles(): array;
}Methods
getMethod
public Request::getMethod(): stringReturns the HTTP method: GET, POST, PUT, DELETE, etc.
getUri
public Request::getUri(): stringReturns the full request URI including the query string (e.g. /api/users?page=2).
getHeader
public Request::getHeader(string $name): ?stringReturns a single header value by name, or null if the header is not present. Header names are case-insensitive.
getHeaders
public Request::getHeaders(): arrayReturns all headers as an associative array name => value. When a header has multiple values they are joined with , .
getBody
public Request::getBody(): stringReturns the full request body as a string. The body is read once and cached — subsequent calls return the same value.
getQueryParams
public Request::getQueryParams(): arrayReturns the parsed and URL-decoded query string as an associative array.
getCookies
public Request::getCookies(): arrayReturns cookies parsed from the Cookie header as an associative array name => value.
getHost
public Request::getHost(): stringReturns the value of the Host header.
getRemoteAddr
public Request::getRemoteAddr(): stringReturns the client address in ip:port format.
getScheme
public Request::getScheme(): stringReturns http or https.
getProtocolVersion
public Request::getProtocolVersion(): stringReturns the HTTP protocol version, e.g. HTTP/1.1 or HTTP/2.0.
getParsedBody
public Request::getParsedBody(): arrayReturns form fields from application/x-www-form-urlencoded and multipart/form-data bodies as an associative array.
getUploadedFiles
public Request::getUploadedFiles(): arrayReturns uploaded files as an array of FrankenPHP\UploadedFile objects. Multiple files for the same field are returned as an array.
Examples
Example #1 Routing by method and path
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$method = $request->getMethod();
$path = parse_url($request->getUri(), PHP_URL_PATH);
if ($method === 'GET' && $path === '/health') {
$response->setStatus(200);
$response->write('OK');
$response->end();
return;
}
$response->setStatus(404);
$response->write('Not Found');
$response->end();
});Example #2 Reading query parameters and cookies
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$cookies = $request->getCookies();
$params = $request->getQueryParams();
$name = $params['name'] ?? $cookies['username'] ?? 'Guest';
$response->setStatus(200);
$response->setHeader('Content-Type', 'text/plain');
$response->write("Hello, {$name}!");
$response->end();
});Example #3 Processing a JSON body
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
if ($request->getMethod() !== 'POST') {
$response->setStatus(405);
$response->end();
return;
}
$data = json_decode($request->getBody(), true);
$response->setStatus(200);
$response->setHeader('Content-Type', 'application/json');
$response->write(json_encode(['received' => $data]));
$response->end();
});See Also
- FrankenPHP\Response -- Building and sending responses
- FrankenPHP\UploadedFile -- Working with uploaded files
- FrankenPHP Integration Guide -- Installation, configuration, and deployment