HttpStatus
in package
Class HttpStatus
A utility class providing constants for all standard HTTP status codes, along with helper methods for determining the category of a given status code and retrieving the corresponding human-readable message.
Tags
Table of Contents
Constants
- ACCEPTED = 202
- BAD_GATEWAY = 502
- BAD_REQUEST = 400
- CONFLICT = 409
- CONTINUE = 100
- CREATED = 201
- EXPECTATION_FAILED = 417
- FORBIDDEN = 403
- FOUND = 302
- GATEWAY_TIMEOUT = 504
- GONE = 410
- HTTP_VERSION_NOT_SUPPORTED = 505
- IM_A_TEAPOT = 418
- INTERNAL_SERVER_ERROR = 500
- LENGTH_REQUIRED = 411
- METHOD_NOT_ALLOWED = 405
- MOVED_PERMANENTLY = 301
- MULTIPLE_CHOICES = 300
- NO_CONTENT = 204
- NON_AUTHORITATIVE_INFORMATION = 203
- NOT_ACCEPTABLE = 406
- NOT_FOUND = 404
- NOT_IMPLEMENTED = 501
- NOT_MODIFIED = 304
- OK = 200
- PARTIAL_CONTENT = 206
- PAYLOAD_TOO_LARGE = 413
- PAYMENT_REQUIRED = 402
- PERMANENT_REDIRECT = 308
- PRECONDITION_FAILED = 412
- PROXY_AUTHENTICATION_REQUIRED = 407
- RANGE_NOT_SATISFIABLE = 416
- REQUEST_TIMEOUT = 408
- RESET_CONTENT = 205
- SEE_OTHER = 303
- SERVICE_UNAVAILABLE = 503
- SWITCHING_PROTOCOLS = 101
- TEMPORARY_REDIRECT = 307
- TOO_MANY_REQUESTS = 429
- UNAUTHORIZED = 401
- UNSUPPORTED_MEDIA_TYPE = 415
- URI_TOO_LONG = 414
- USE_PROXY = 305
Properties
- $textMapCache : array<string|int, mixed>|null
Methods
- all() : array<int, string>
- Returns an array of all defined HTTP status codes and their reason phrases.
- allCodes() : array<string|int, int>
- Returns all known HTTP status codes as an array. useful for enum-style dropdowns or schema gen
- asHtmlSelect() : string
- Generates an HTML <select> element of all HTTP codes grouped by category for dashboards or admin panels, etc.
- category() : string
- Returns the category of the HTTP status code as a string.
- codesInCategory() : array<int, string>
- Returns all HTTP status codes for a given category.
- description() : string
- Returns a longer-form description of the HTTP status code.
- docsUrl() : string
- Returns a link to documentation for the given HTTP status code.
- fromText() : int|null
- Attempts to find the HTTP status code for a given reason phrase.
- grouped() : array<string, array<int, string>>
- Returns all status codes grouped by category.
- httpEmoji() : string|null
- Returns a visual emoji cue for a given status code.
- isAuthRelated() : bool
- Determines if the status code relates to authentication or authorization.
- isCacheable() : bool
- Determines if the response is generally considered cacheable.
- isClientError() : bool
- Determines whether the given HTTP status code indicates a client error (4xx).
- isCustom() : bool
- Checks whether a status code is non-standard/custom.
- isDeprecated() : bool
- Checks whether the status code is deprecated or rarely used.
- isEmptyResponse() : bool
- Determines if the response should not include a body.
- isError() : bool
- Determines whether the given HTTP status code is any kind of error (4xx or 5xx).
- isFinal() : bool
- Determines if the status code represents a final response.
- isIdempotent() : bool
- Indicates if the HTTP method producing this response should be idempotent.
- isInformational() : bool
- Determines whether the given HTTP status code is informational (1xx).
- isInternetError() : bool
- Checks if the code suggests an internet or upstream connection failure.
- isRedirect() : bool
- Determines whether the given HTTP status code is a redirection (3xx).
- isRetryable() : bool
- Determines if the given HTTP status code is considered retryable.
- isSafe() : bool
- Checks if the status code implies a safe, cacheable, idempotent result. Is it safe to retry automatically? Not the same as isRetryable() — this is from an HTTP protocol/client perspective.
- isServerError() : bool
- Determines whether the given HTTP status code indicates a server error (5xx).
- isStandard() : bool
- Checks if the given code is officially defined in the HTTP standard.
- isSuccess() : bool
- Determines whether the given HTTP status code indicates success (2xx).
- isValid() : bool
- Determines if the status code is one of the defined HTTP status codes.
- loggableSeverity() : string
- Suggests a PSR-3 log severity level for the given HTTP code.
- matches() : bool
- Compares a flexible input (string, int, slug) against an HTTP status code.
- meta() : array<string, mixed>
- Returns a structured metadata array for the given HTTP status code.
- random() : int
- Returns a random valid HTTP status code. Could be useful in testing? Unlikely, but hey.
- recommendedAction() : string|null
- Suggests a developer or operations action for the given status code.
- recommendRetryDelay() : int|null
- Recommends a retry delay (in seconds) for retryable status codes.
- sendHeader() : void
- Sends the HTTP status header using the given code.
- series() : int
- Returns the HTTP status series (e.g., 2 for 2xx).
- statusClass() : string
- Returns a CSS class (e.g., for badge coloring) based on HTTP code category.
- statusLine() : string
- Returns a full HTTP status line (e.g. "HTTP/1.1 404 Not Found").
- text() : string
- Returns the standard reason phrase for a given HTTP status code.
- textMap() : array<string|int, mixed>
- toArray() : array<int, string>
- Returns an associative array of all HTTP status codes and their reason phrases.
- toJson() : string
- Returns a JSON-encoded string of all HTTP status codes and their reason phrases.
- toSpeech() : string
- Returns a spoken-friendly version of the HTTP status.
Constants
ACCEPTED
public
mixed
ACCEPTED
= 202
BAD_GATEWAY
public
mixed
BAD_GATEWAY
= 502
BAD_REQUEST
public
mixed
BAD_REQUEST
= 400
CONFLICT
public
mixed
CONFLICT
= 409
CONTINUE
public
mixed
CONTINUE
= 100
CREATED
public
mixed
CREATED
= 201
EXPECTATION_FAILED
public
mixed
EXPECTATION_FAILED
= 417
FORBIDDEN
public
mixed
FORBIDDEN
= 403
FOUND
public
mixed
FOUND
= 302
GATEWAY_TIMEOUT
public
mixed
GATEWAY_TIMEOUT
= 504
GONE
public
mixed
GONE
= 410
HTTP_VERSION_NOT_SUPPORTED
public
mixed
HTTP_VERSION_NOT_SUPPORTED
= 505
IM_A_TEAPOT
public
mixed
IM_A_TEAPOT
= 418
INTERNAL_SERVER_ERROR
public
mixed
INTERNAL_SERVER_ERROR
= 500
LENGTH_REQUIRED
public
mixed
LENGTH_REQUIRED
= 411
METHOD_NOT_ALLOWED
public
mixed
METHOD_NOT_ALLOWED
= 405
MOVED_PERMANENTLY
public
mixed
MOVED_PERMANENTLY
= 301
MULTIPLE_CHOICES
public
mixed
MULTIPLE_CHOICES
= 300
NO_CONTENT
public
mixed
NO_CONTENT
= 204
NON_AUTHORITATIVE_INFORMATION
public
mixed
NON_AUTHORITATIVE_INFORMATION
= 203
NOT_ACCEPTABLE
public
mixed
NOT_ACCEPTABLE
= 406
NOT_FOUND
public
mixed
NOT_FOUND
= 404
NOT_IMPLEMENTED
public
mixed
NOT_IMPLEMENTED
= 501
NOT_MODIFIED
public
mixed
NOT_MODIFIED
= 304
OK
public
mixed
OK
= 200
PARTIAL_CONTENT
public
mixed
PARTIAL_CONTENT
= 206
PAYLOAD_TOO_LARGE
public
mixed
PAYLOAD_TOO_LARGE
= 413
PAYMENT_REQUIRED
public
mixed
PAYMENT_REQUIRED
= 402
PERMANENT_REDIRECT
public
mixed
PERMANENT_REDIRECT
= 308
PRECONDITION_FAILED
public
mixed
PRECONDITION_FAILED
= 412
PROXY_AUTHENTICATION_REQUIRED
public
mixed
PROXY_AUTHENTICATION_REQUIRED
= 407
RANGE_NOT_SATISFIABLE
public
mixed
RANGE_NOT_SATISFIABLE
= 416
REQUEST_TIMEOUT
public
mixed
REQUEST_TIMEOUT
= 408
RESET_CONTENT
public
mixed
RESET_CONTENT
= 205
SEE_OTHER
public
mixed
SEE_OTHER
= 303
SERVICE_UNAVAILABLE
public
mixed
SERVICE_UNAVAILABLE
= 503
SWITCHING_PROTOCOLS
public
mixed
SWITCHING_PROTOCOLS
= 101
TEMPORARY_REDIRECT
public
mixed
TEMPORARY_REDIRECT
= 307
TOO_MANY_REQUESTS
public
mixed
TOO_MANY_REQUESTS
= 429
UNAUTHORIZED
public
mixed
UNAUTHORIZED
= 401
UNSUPPORTED_MEDIA_TYPE
public
mixed
UNSUPPORTED_MEDIA_TYPE
= 415
URI_TOO_LONG
public
mixed
URI_TOO_LONG
= 414
USE_PROXY
public
mixed
USE_PROXY
= 305
Properties
$textMapCache
private
static array<string|int, mixed>|null
$textMapCache
= null
Methods
all()
Returns an array of all defined HTTP status codes and their reason phrases.
public
static all() : array<int, string>
Return values
array<int, string>allCodes()
Returns all known HTTP status codes as an array. useful for enum-style dropdowns or schema gen
public
static allCodes() : array<string|int, int>
Return values
array<string|int, int>asHtmlSelect()
Generates an HTML <select> element of all HTTP codes grouped by category for dashboards or admin panels, etc.
public
static asHtmlSelect([string $name = 'http_status' ]) : string
Parameters
- $name : string = 'http_status'
Return values
stringcategory()
Returns the category of the HTTP status code as a string.
public
static category(int $code) : string
Parameters
- $code : int
Return values
string —One of: 'informational', 'success', 'redirect', 'client_error', 'server_error', or 'unknown'
codesInCategory()
Returns all HTTP status codes for a given category.
public
static codesInCategory(string $category) : array<int, string>
Parameters
- $category : string
-
One of: 'informational', 'success', 'redirect', 'client_error', 'server_error'
Return values
array<int, string> —Map of code => reason
description()
Returns a longer-form description of the HTTP status code.
public
static description(int $code) : string
Useful for logging, debugging, API documentation, or user-facing error pages.
Parameters
- $code : int
-
The HTTP status code.
Return values
string —A detailed description of what the status code means.
docsUrl()
Returns a link to documentation for the given HTTP status code.
public
static docsUrl(int $code) : string
Parameters
- $code : int
Return values
stringfromText()
Attempts to find the HTTP status code for a given reason phrase.
public
static fromText(string $text) : int|null
Parameters
- $text : string
Return values
int|nullgrouped()
Returns all status codes grouped by category.
public
static grouped() : array<string, array<int, string>>
Return values
array<string, array<int, string>>httpEmoji()
Returns a visual emoji cue for a given status code.
public
static httpEmoji(int $code) : string|null
Parameters
- $code : int
Return values
string|nullisAuthRelated()
Determines if the status code relates to authentication or authorization.
public
static isAuthRelated(int $code) : bool
Parameters
- $code : int
Return values
boolisCacheable()
Determines if the response is generally considered cacheable.
public
static isCacheable(int $code) : bool
Parameters
- $code : int
Return values
boolisClientError()
Determines whether the given HTTP status code indicates a client error (4xx).
public
static isClientError(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if client error, false otherwise.
isCustom()
Checks whether a status code is non-standard/custom.
public
static isCustom(int $code) : bool
Parameters
- $code : int
Return values
boolisDeprecated()
Checks whether the status code is deprecated or rarely used.
public
static isDeprecated(int $code) : bool
Parameters
- $code : int
Return values
boolisEmptyResponse()
Determines if the response should not include a body.
public
static isEmptyResponse(int $code) : bool
Parameters
- $code : int
Return values
boolisError()
Determines whether the given HTTP status code is any kind of error (4xx or 5xx).
public
static isError(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if client or server error, false otherwise.
isFinal()
Determines if the status code represents a final response.
public
static isFinal(int $code) : bool
Parameters
- $code : int
Return values
boolisIdempotent()
Indicates if the HTTP method producing this response should be idempotent.
public
static isIdempotent(int $code) : bool
Parameters
- $code : int
Return values
boolisInformational()
Determines whether the given HTTP status code is informational (1xx).
public
static isInformational(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if informational, false otherwise.
isInternetError()
Checks if the code suggests an internet or upstream connection failure.
public
static isInternetError(int $code) : bool
Parameters
- $code : int
Return values
boolisRedirect()
Determines whether the given HTTP status code is a redirection (3xx).
public
static isRedirect(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if redirect, false otherwise.
isRetryable()
Determines if the given HTTP status code is considered retryable.
public
static isRetryable(int $code) : bool
Typically 429, 502, 503, 504.
Parameters
- $code : int
Return values
boolisSafe()
Checks if the status code implies a safe, cacheable, idempotent result. Is it safe to retry automatically? Not the same as isRetryable() — this is from an HTTP protocol/client perspective.
public
static isSafe(int $code) : bool
Parameters
- $code : int
Return values
boolisServerError()
Determines whether the given HTTP status code indicates a server error (5xx).
public
static isServerError(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if server error, false otherwise.
isStandard()
Checks if the given code is officially defined in the HTTP standard.
public
static isStandard(int $code) : bool
Parameters
- $code : int
Return values
boolisSuccess()
Determines whether the given HTTP status code indicates success (2xx).
public
static isSuccess(int $code) : bool
Parameters
- $code : int
-
The HTTP status code.
Return values
bool —True if successful, false otherwise.
isValid()
Determines if the status code is one of the defined HTTP status codes.
public
static isValid(int $code) : bool
Parameters
- $code : int
Return values
boolloggableSeverity()
Suggests a PSR-3 log severity level for the given HTTP code.
public
static loggableSeverity(int $code) : string
Parameters
- $code : int
Return values
stringmatches()
Compares a flexible input (string, int, slug) against an HTTP status code.
public
static matches(int|string $input, int $code) : bool
Parameters
- $input : int|string
- $code : int
Return values
boolmeta()
Returns a structured metadata array for the given HTTP status code.
public
static meta(int $code) : array<string, mixed>
Parameters
- $code : int
Return values
array<string, mixed>random()
Returns a random valid HTTP status code. Could be useful in testing? Unlikely, but hey.
public
static random() : int
..
Return values
intrecommendedAction()
Suggests a developer or operations action for the given status code.
public
static recommendedAction(int $code) : string|null
Parameters
- $code : int
Return values
string|nullrecommendRetryDelay()
Recommends a retry delay (in seconds) for retryable status codes.
public
static recommendRetryDelay(int $code) : int|null
Parameters
- $code : int
Return values
int|null —Suggested delay in seconds, or null if not retryable.
sendHeader()
Sends the HTTP status header using the given code.
public
static sendHeader(int $code[, string $protocol = 'HTTP/1.1' ]) : void
Parameters
- $code : int
- $protocol : string = 'HTTP/1.1'
-
HTTP version (default: HTTP/1.1)
series()
Returns the HTTP status series (e.g., 2 for 2xx).
public
static series(int $code) : int
Parameters
- $code : int
Return values
intstatusClass()
Returns a CSS class (e.g., for badge coloring) based on HTTP code category.
public
static statusClass(int $code) : string
Parameters
- $code : int
Return values
stringstatusLine()
Returns a full HTTP status line (e.g. "HTTP/1.1 404 Not Found").
public
static statusLine(int $code[, string $protocol = 'HTTP/1.1' ]) : string
Parameters
- $code : int
- $protocol : string = 'HTTP/1.1'
-
HTTP version (default: HTTP/1.1)
Return values
stringtext()
Returns the standard reason phrase for a given HTTP status code.
public
static text(int $code) : string
Parameters
- $code : int
-
The HTTP status code (e.g. 200, 404, 500).
Return values
string —The corresponding reason phrase or 'Unknown Status' if unrecognized.
textMap()
public
static textMap() : array<string|int, mixed>
Return values
array<string|int, mixed>toArray()
Returns an associative array of all HTTP status codes and their reason phrases.
public
static toArray() : array<int, string>
Useful for displaying or serializing the complete list of supported HTTP status codes.
Return values
array<int, string> —An array where the keys are HTTP status codes and the values are their reason phrases.
toJson()
Returns a JSON-encoded string of all HTTP status codes and their reason phrases.
public
static toJson([bool $pretty = false ]) : string
This is helpful for APIs, documentation endpoints, or front-end tooling that needs access to the status code map.
Parameters
- $pretty : bool = false
-
If true, the JSON will be pretty-printed for readability.
Return values
string —A JSON string representing all known HTTP status codes and their descriptions.
toSpeech()
Returns a spoken-friendly version of the HTTP status.
public
static toSpeech(int $code) : string
Parameters
- $code : int