FrankenPHP\Response
(True Async 0.6+)
The Response object is passed to your handler callback by HttpServer::onRequest(). It provides methods to set the status code, headers, body, and send the response to the client.
Important: always call
end()to send the response, even when the body is empty.write()buffers data in the object;end()sends everything to the client. Omittingend()will hang the request.
Class Synopsis
namespace FrankenPHP;
class Response
{
public function setStatus(int $code): void;
public function getStatus(): int;
public function setHeader(string $name, string $value): void;
public function addHeader(string $name, string $value): void;
public function removeHeader(string $name): void;
public function getHeader(string $name): ?string;
public function getHeaders(): array;
public function isHeadersSent(): bool;
public function redirect(string $url, int $code = 302): void;
public function write(string $data): void;
public function end(): void;
}Methods
setStatus
public Response::setStatus(int $code): voidSet the HTTP status code. Default is 200.
getStatus
public Response::getStatus(): intReturns the current status code.
setHeader
public Response::setHeader(string $name, string $value): voidSet a header, replacing any existing values for that name.
addHeader
public Response::addHeader(string $name, string $value): voidAppend a header value. Use this for headers that can have multiple values, such as Set-Cookie.
removeHeader
public Response::removeHeader(string $name): voidRemove a previously set header.
getHeader
public Response::getHeader(string $name): ?stringReturns the first value of a header, or null if not set.
getHeaders
public Response::getHeaders(): arrayReturns all headers as an associative array name => [values...].
isHeadersSent
public Response::isHeadersSent(): boolReturns true if end() has already been called.
redirect
public Response::redirect(string $url, int $code = 302): voidSets the Location header and the status code. You still need to call end() after.
write
public Response::write(string $data): voidBuffers response body data. Can be called multiple times — chunks are concatenated.
end
public Response::end(): voidSends the status code, headers, and buffered body to the client. Must be called to complete the response. After end(), further calls to write() or header methods have no effect.
Examples
Example #1 Basic text response
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$response->setStatus(200);
$response->setHeader('Content-Type', 'text/plain');
$response->write('Hello, World!');
$response->end();
});Example #2 JSON API response
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$data = ['status' => 'ok', 'time' => time()];
$response->setStatus(200);
$response->setHeader('Content-Type', 'application/json');
$response->write(json_encode($data));
$response->end();
});Example #3 Setting cookies and redirecting
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$cookies = $request->getCookies();
if (!isset($cookies['session'])) {
$response->addHeader('Set-Cookie', 'session=abc123; Path=/; HttpOnly');
$response->addHeader('Set-Cookie', 'theme=dark; Path=/');
$response->redirect('/welcome');
$response->end();
return;
}
$response->setStatus(200);
$response->setHeader('Content-Type', 'text/plain');
$response->write('Welcome back!');
$response->end();
});Example #4 Streaming-style writes
<?php
use FrankenPHP\HttpServer;
use FrankenPHP\Request;
use FrankenPHP\Response;
HttpServer::onRequest(function (Request $request, Response $response): void {
$response->setStatus(200);
$response->setHeader('Content-Type', 'text/plain');
// Multiple write() calls — all data is buffered and sent at end()
$response->write("Line 1\n");
$response->write("Line 2\n");
$response->write("Line 3\n");
$response->end();
});See Also
- FrankenPHP\Request -- Reading request data
- FrankenPHP\UploadedFile -- Working with uploaded files
- FrankenPHP Integration Guide -- Installation, configuration, and deployment