Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Http\Response->status(int $status, string $reason = ''): bool
The HTTP status code to set for the response
A message for the reason of the status code, optional
If successful, true
is returned otherwise false
Set the HTTP status code that will be sent to the client. This method has an alias of setStatusCode()
.
If a status code is not set and the server does not encounter any errors, the default value is 200
.
You don't have to set this, the OpenSwoole server will set the status code respectively but this gives you more control. You must only set valid HTTP status codes like 200, 502, 301, 404, 401 etc.
If an invalid status code is set, OpenSwoole will reset the value to 200. However, if a HTTP status code $reason
is given, you may set any status code such as 499 or 856.
This method must be called before the
$response->end
method.
<?php
$server->on('Request', function(OpenSwoole\Server\Request $request, OpenSwoole\Server\Response $response)
{
// Normal HTTP status code
$response->status(401);
// Custom status code with a reason message
$response->status(499, 'Custom status code message with a reason');
});