【问题标题】:Codeigniter output class doesn't contain http 429 status codeCodeigniter 输出类不包含 http 429 状态码
【发布时间】:2016-09-15 03:48:09
【问题描述】:

我一直在使用 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 (@987654323) 中定义的其他状态代码@)。这意味着我无法发送 429(请求过多)状态码。

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

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      您可以通过在 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-11-02
        • 1970-01-01
        • 2021-08-31
        • 2018-08-12
        • 2021-08-19
        • 1970-01-01
        • 2018-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多