OpenSwoole\Http\Response->status()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Http\Response->status(int $status, string $reason = ''): bool

Parameters

status

The HTTP status code to set for the response

reason

A message for the reason of the status code, optional

Return

bool

If successful, true is returned otherwise false

Description

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.

Example

<?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');
});
Last updated on September 20, 2022