irap/http

一个使错误代码更易于使用的包。

1.1.0 2024-01-10 16:40 UTC

This package is auto-updated.

Last update: 2024-09-10 18:28:58 UTC


README

要使用此包定义,您需要手动包含以下内容:

用法

使用以下命令将此包下载到您的项目中:

composer require irap/http-codes

以下是使用此包返回API请求的404 JSON响应的示例。

<?php
http_response_code(iRAP/Http/HttpCode::NOT_FOUND);

$body = array(
    "result" => "error",
    "message" => "Resource not found.",
);

header('Content-Type: application/json');
die(json_encode($body));

或者...

<?php
use iRAP\Http\HttpCode;

http_response_code(HttpCode::NOT_FOUND);

$body = array(
    "result" => "error",
    "message" => "Resource not found.",
);

header('Content-Type: application/json');
die(json_encode($body));

还有一种方法可以根据这个检查传递的参数是否为有效的HTTP状态码。

<?php
use iRAP\Http\HttpCode;

if (HttpCode::isValid(404)) {
    echo "Valid HTTP status code";
} else {
    echo "Invalid HTTP status code";
}