0

我一直在使用 codeigniter 进行开发并利用输出类 ( https://www.codeigniter.com/user_guide/libraries/output.html ) 来轻松发送正确的状态代码、标头和 json 响应。

但是,在尝试调试我遇到的问题后,我意识到 set_status_header 函数实现了 RFC 2616 ( https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html )中的状态代码但不是 RFC 6585 ( https://www.rfc-editor.org/rfc/rfc6585 ) 中定义的附加状态代码。这意味着我无法发送 429(请求太多)状态码。

是否有支持这个的输出类的更新版本或者我应该只使用 php 的 header() 函数来处理这个?

4

2 回答 2

1

决定使用 http_response_code() 并且仍然使用输出类,所以我的代码如下所示:

http_response_code(429);
return $this->output
    ->set_header("Retry-After: " . $resp['retry_after'])
    ->set_content_type('application/json')
    ->set_output($json_result);

这很好,只是有点烦人,它与正常情况不同:

return $this->output
    ->set_status_header('401')
    ->set_content_type('application/json')
    ->set_output($json_result);
于 2016-05-18T15:06:50.943 回答
0

您可以通过在 application/core 中创建 MY_Output.php 并覆盖来扩展核心输出类set_status_header()

MY_Output.php

class MY_Output extends CI_Output {
    public function __construct() {
        parent::__construct();
    }

    public function set_status_header($code = 200, $text = '') {
        // copied helper function set_status_header() code from system/core/Common.php
        if (is_cli())
        {
            return;
        }

        if (empty($code) OR ! is_numeric($code))
        {
            show_error('Status codes must be numeric', 500);
        }

        if (empty($text))
        {
            is_int($code) OR $code = (int) $code;
            // Add your status codes/text in this array below
            $stati = array(
                100 => 'Continue',
                101 => 'Switching Protocols',

                200 => 'OK',
                201 => 'Created',
                202 => 'Accepted',
                203 => 'Non-Authoritative Information',
                204 => 'No Content',
                205 => 'Reset Content',
                206 => 'Partial Content',

                300 => 'Multiple Choices',
                301 => 'Moved Permanently',
                302 => 'Found',
                303 => 'See Other',
                304 => 'Not Modified',
                305 => 'Use Proxy',
                307 => 'Temporary Redirect',

                400 => 'Bad Request',
                401 => 'Unauthorized',
                402 => 'Payment Required',
                403 => 'Forbidden',
                404 => 'Not Found',
                405 => 'Method Not Allowed',
                406 => 'Not Acceptable',
                407 => 'Proxy Authentication Required',
                408 => 'Request Timeout',
                409 => 'Conflict',
                410 => 'Gone',
                411 => 'Length Required',
                412 => 'Precondition Failed',
                413 => 'Request Entity Too Large',
                414 => 'Request-URI Too Long',
                415 => 'Unsupported Media Type',
                416 => 'Requested Range Not Satisfiable',
                417 => 'Expectation Failed',
                422 => 'Unprocessable Entity',
                429 => 'Too Many Requests',

                500 => 'Internal Server Error',
                501 => 'Not Implemented',
                502 => 'Bad Gateway',
                503 => 'Service Unavailable',
                504 => 'Gateway Timeout',
                505 => 'HTTP Version Not Supported'
            );

            if (isset($stati[$code]))
            {
                $text = $stati[$code];
            }
            else
            {
                show_error('No status text available. Please check your status code number or supply your own message text.', 500);
            }
        }

        if (strpos(PHP_SAPI, 'cgi') === 0)
        {
            header('Status: '.$code.' '.$text, TRUE);
        }
        else
        {
            $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
            header($server_protocol.' '.$code.' '.$text, TRUE, $code);
        }

        return $this;
    }
}
于 2016-05-18T15:22:24.017 回答