2016-05-18 15 views
0

コードネイターを開発用に使用していて、出力クラス(https://ellislab.com/codeigniter/user-guide/libraries/output.html)を使用して、正しいステータスコード、ヘッダー、およびjson応答を簡単に送信できます。Codeigniter出力クラスにはhttp 429のステータスコードが含まれていません

しかし、問題をデバッグしようとした後、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()関数を使うべきですか?私のコードは次のようになりますので、

答えて

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); 
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; 
    } 
} 
関連する問題