note 128649 added to function.http-response-code
| From: | theking2 at main dot php dot net | Date: | Fri, 30 Jun 2023 07:53:45 +0000 |
| Subject: | note 128649 added to function.http-response-code | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220609@lists.php.net to get a copy of this message | ||
I use a backed enum (8.2+) to handle responses:
For status codes:
<?php
enum HttpStatusCode: int
{
case Continue = 100;
case SwitchingProtocols = 101;
case OK = 200;
case Created = 201;
case Accepted = 202;
case NonAuthoritativeInformation = 203;
case NoContent = 204;
case ResetContent = 205;
case PartialContent = 206;
case MultipleChoices = 300;
case MovedPermanently = 301;
case Found = 302;
case SeeOther = 303;
case NotModified = 304;
case UseProxy = 305;
case TemporaryRedirect = 307;
case BadRequest = 400;
case Unauthorized = 401;
case PaymentRequired = 402;
case Forbidden = 403;
case NotFound = 404;
case MethodNotAllowed = 405;
case NotAcceptable = 406;
case ProxyAuthenticationRequired = 407;
case RequestTimeout = 408;
case Conflict = 409;
case Gone = 410;
case LengthRequired = 411;
case PreconditionFailed = 412;
case RequestEntityTooLarge = 413;
case RequestURITooLong = 414;
case UnsupportedMediaType = 415;
case RequestedRangeNotSatisfiable = 416;
case ExpectationFailed = 417;
case InternalServerError = 500;
case NotImplemented = 501;
case BadGateway = 502;
case ServiceUnavailable = 503;
case GatewayTimeout = 504;
case HTTPVersionNotSupported = 505;
};
?>
and to get the status text:
<?php
private static function decodeHttpResponse( HttpStatusCode $statusCode ): string
{
return match ( $statusCode ) {
HttpStatusCode::Continue => 'Continue',
HttpStatusCode::SwitchingProtocols => 'Switching Protocols',
HttpStatusCode::OK => 'OK',
HttpStatusCode::Created => 'Created',
HttpStatusCode::Accepted => 'Accepted',
HttpStatusCode::NonAuthoritativeInformation => 'Non-Authoritative Information',
HttpStatusCode::NoContent => 'No Content',
HttpStatusCode::ResetContent => 'Reset Content',
HttpStatusCode::PartialContent => 'Partial Content',
HttpStatusCode::MultipleChoices => 'Multiple Choices',
HttpStatusCode::MovedPermanently => 'Moved Permanently',
HttpStatusCode::Found => 'Found',
HttpStatusCode::SeeOther => 'See Other',
HttpStatusCode::NotModified => 'Not Modified',
HttpStatusCode::UseProxy => 'Use Proxy',
HttpStatusCode::TemporaryRedirect => 'Temporary Redirect',
HttpStatusCode::BadRequest => 'Bad Request',
HttpStatusCode::Unauthorized => 'Unauthorized',
HttpStatusCode::PaymentRequired => 'Payment Required',
HttpStatusCode::Forbidden => 'Forbidden',
HttpStatusCode::NotFound => 'Not Found',
HttpStatusCode::MethodNotAllowed => 'Method Not Allowed',
HttpStatusCode::NotAcceptable => 'Not Acceptable',
HttpStatusCode::ProxyAuthenticationRequired => 'Proxy Authentication Required',
HttpStatusCode::RequestTimeout => 'Request Timeout',
HttpStatusCode::Conflict => 'Conflict',
HttpStatusCode::Gone => 'Gone',
HttpStatusCode::LengthRequired => 'Length Required',
HttpStatusCode::PreconditionFailed => 'Precondition Failed',
HttpStatusCode::RequestEntityTooLarge => 'Request Entity Too Large',
HttpStatusCode::RequestURITooLong => 'Request-URI Too Long',
HttpStatusCode::UnsupportedMediaType => 'Unsupported Media Type',
HttpStatusCode::RequestedRangeNotSatisfiable => 'Requested Range Not Satisfiable',
HttpStatusCode::ExpectationFailed => 'Expectation Failed',
HttpStatusCode::InternalServerError => 'Internal Server Error',
HttpStatusCode::NotImplemented => 'Not Implemented',
HttpStatusCode::BadGateway => 'Bad Gateway',
HttpStatusCode::ServiceUnavailable => 'Service Unavailable',
HttpStatusCode::GatewayTimeout => 'Gateway Timeout',
HttpStatusCode::HTTPVersionNotSupported => 'HTTP Version Not Supported',
default => 'Unknown HTTP status code'
};
}
?>
The header can than be send as:
<?php
public static function sendStatusCode( HttpStatusCode $httpStatusCode ): void
{
header(
sprintf(
'HTTP/1.1 %d %s',
$httpStatusCode->value,
self::decodeHttpResponse( $httpStatusCode )
)
);
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.168 (proxied: 185.163.92.217)
----
Manual Page -- https://php.net/manual/en/function.http-response-code.php
Edit -- https://main.php.net/note/edit/128649
Del: integrated -- https://main.php.net/note/delete/128649/integrated
Del: useless -- https://main.php.net/note/delete/128649/useless
Del: bad code -- https://main.php.net/note/delete/128649/bad+code
Del: spam -- https://main.php.net/note/delete/128649/spam
Del: non-english -- https://main.php.net/note/delete/128649/non-english
Del: in docs -- https://main.php.net/note/delete/128649/in+docs
Del: other reasons-- https://main.php.net/note/delete/128649
Reject -- https://main.php.net/note/reject/128649
Search -- https://main.php.net/manage/user-notes.php