Swoole\Http\Response->status()

Declaration

<?php
Swoole\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 Swoole 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, Swoole 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(Swoole/Server/Request $request, Swoole/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');
});