Codeigniter输出类不包含http 429状态代码

时间:2016-05-18 14:57:09

标签: php codeigniter http-headers http-status-code-429

我一直在使用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://tools.ietf.org/html/rfc6585)。这意味着我无法发送429(请求太多)状态代码。

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

2 个答案:

答案 0 :(得分:0)

决定使用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);

答案 1 :(得分: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;
    }
}