2013-08-06 213 views
1

我用笨的REST API(作者:philsturgeon參考網址:​​)笨REST API響應登錄

我與給定的API代碼查詢/問題。

我知道有一個函數和對數表「日誌」來存儲API請求(該請求的參數)。這很好。

我想知道有沒有辦法讓來自API的響應,並將其存儲在同一個表。

換句話說,在創建請求日誌和它的默認內置功能的笨。我還需要幫助來存儲響應。有沒有辦法?

+0

你可以在這裏粘貼你的代碼嗎? – senthilbp

+0

@senthilbp - 感謝您的回覆。我在這裏添加了我的代碼 http://pastebin.com/embed_iframe.php?i=tuGCKx70 請檢查並引導我,如果你可以 – hjaffer2001

回答

2

首先,添加了通響應另一個參數_log_request功能即

_log_request($authorized = FALSE)

_log_request($authorized = FALSE,$response="")

你可以通過你的響應輸出響應函數即

$this->_log_request($authorized = TRUE,$output);

添加 '迴應' 在你的數據庫表字段來存儲響應

下面是更新後的代碼:

// Request Parameter Log function 
protected function _log_request($authorized = FALSE,$response="") 
    { 
     return $this->rest->db->insert(config_item('rest_logs_table'), array(
        'uri' => $this->uri->uri_string(), 
        'method' => $this->request->method, 
        'params' => $this->_args ? (config_item('rest_logs_json_params') ? json_encode($this->_args) : serialize($this->_args)) : null, 
        'ip_address' => $this->input->ip_address(), 
        'time' => function_exists('now') ? now() : time(), 
        'authorized' => $authorized, 
        'response' => $response 
       )); 
    } 

// Response function 
public function response($data = array(), $http_code = null) 
    { 
     global $CFG; 

     // If data is empty and not code provide, error and bail 
     if (empty($data) && $http_code === null) 
     { 
      $http_code = 404; 

      // create the output variable here in the case of $this->response(array()); 
      $output = NULL; 
     } 

     // If data is empty but http code provided, keep the output empty 
     else if (empty($data) && is_numeric($http_code)) 
     { 
      $output = NULL; 
     } 

     // Otherwise (if no data but 200 provided) or some data, carry on camping! 
     else 
     { 
      // Is compression requested? 
      if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE) 
      { 
       if (extension_loaded('zlib')) 
       { 
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) 
        { 
         ob_start('ob_gzhandler'); 
        } 
       } 
      } 

      is_numeric($http_code) OR $http_code = 200; 

      // If the format method exists, call and return the output in that format 
      if (method_exists($this, '_format_'.$this->response->format)) 
      { 
       // Set the correct format header 
       header('Content-Type: '.$this->_supported_formats[$this->response->format]); 

       $output = $this->{'_format_'.$this->response->format}($data); 
      } 

      // If the format method exists, call and return the output in that format 
      elseif (method_exists($this->format, 'to_'.$this->response->format)) 
      { 
       // Set the correct format header 
       header('Content-Type: '.$this->_supported_formats[$this->response->format]); 

       $output = $this->format->factory($data)->{'to_'.$this->response->format}(); 
      } 

      // Format not supported, output directly 
      else 
      { 
       $output = $data; 
      } 
     } 

     header('HTTP/1.1: ' . $http_code); 
     header('Status: ' . $http_code); 

     // If zlib.output_compression is enabled it will compress the output, 
     // but it will not modify the content-length header to compensate for 
     // the reduction, causing the browser to hang waiting for more data. 
     // We'll just skip content-length in those cases. 
     if (! $this->_zlib_oc && ! $CFG->item('compress_output')) 
     { 
      header('Content-Length: ' . strlen($output)); 
     } 
     if (config_item('rest_enable_logging')) 
     { 
     $this->_log_request($authorized = TRUE,$output); 
     } 

     exit($output); 
    } 
+0

你是救世主。感謝您的解釋和代碼。它真的有效!你真棒!再次感謝 – hjaffer2001

1

偉大的建議,senthilbp。但我認爲,更好的讓每個API請求只有一條記錄,用你的將被創建2條記錄與除「響應」的所有相同的字段每個請求的代碼。所以我有選擇。 好的做法是不會改變原始的源代碼,但使用OOP的美麗和繼承自己的類

abstract class MY_REST_Controller extends REST_Controller 
{ 
    /** 
    * PK from $config['rest_logs_table'] table for last inserted log. 
    * 
    * @var integer|null 
    */ 
    protected $_log_id; 

    /** 
    * Log request 
    * 
    * Record the entry for awesomeness purposes 
    * 
    * @param boolean $authorized 
    * @return object 
    */ 
    protected function _log_request($authorized = FALSE) 
    { 
     $res = $this->rest->db->insert(config_item('rest_logs_table'), array(
        'uri' => $this->uri->uri_string(), 
        'method' => $this->request->method, 
        'params' => $this->_args ? (config_item('rest_logs_json_params') ? json_encode($this->_args) : serialize($this->_args)) : null, 
        'api_key' => isset($this->rest->key) ? $this->rest->key : '', 
        'ip_address' => $this->input->ip_address(), 
        'time' => function_exists('now') ? now() : time(), 
        'authorized' => $authorized 
       )); 
     $this->_log_id = $this->db->insert_id(); 
     return $res; 
    } 

    /** 
    * Log request response, update existing record, created by @see _log_request() 
    * 
    * @param string $response 
    * @return object 
    */ 
    protected function _log_response($response="") 
    { 
     if(!$this->_log_id) 
     { 
      return; 
     } 
     $log_data['response'] = $response; 
     $this->rest->db->where('id', $this->_log_id); 
     $this->rest->db->where('response', NULL);// to prevent overwriting 
     return $this->rest->db->update(config_item('rest_logs_table'), $log_data); 
    } 

    /** 
    * Response 
    * 
    * Takes pure data and optionally a status code, then creates the response. 
    * 
    * @param array $data 
    * @param null|int $http_code 
    */ 
    public function response($data = array(), $http_code = null) 
    { 
     global $CFG; 

     // If data is empty and not code provide, error and bail 
     if (empty($data) && $http_code === null) 
     { 
      $http_code = 404; 

      // create the output variable here in the case of $this->response(array()); 
      $output = NULL; 
     } 

     // If data is empty but http code provided, keep the output empty 
     else if (empty($data) && is_numeric($http_code)) 
     { 
      $output = NULL; 
     } 

     // Otherwise (if no data but 200 provided) or some data, carry on camping! 
     else 
     { 
      // Is compression requested? 
      if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE) 
      { 
       if (extension_loaded('zlib')) 
       { 
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) 
        { 
         ob_start('ob_gzhandler'); 
        } 
       } 
      } 

      is_numeric($http_code) OR $http_code = 200; 

      // If the format method exists, call and return the output in that format 
      if (method_exists($this, '_format_'.$this->response->format)) 
      { 
       // Set the correct format header 
       header('Content-Type: '.$this->_supported_formats[$this->response->format]); 

       $output = $this->{'_format_'.$this->response->format}($data); 
      } 

      // If the format method exists, call and return the output in that format 
      elseif (method_exists($this->format, 'to_'.$this->response->format)) 
      { 
       // Set the correct format header 
       header('Content-Type: '.$this->_supported_formats[$this->response->format]); 

       $output = $this->format->factory($data)->{'to_'.$this->response->format}(); 
      } 

      // Format not supported, output directly 
      else 
      { 
       $output = $data; 
      } 
     } 

     header('HTTP/1.1: ' . $http_code); 
     header('Status: ' . $http_code); 

     // If zlib.output_compression is enabled it will compress the output, 
     // but it will not modify the content-length header to compensate for 
     // the reduction, causing the browser to hang waiting for more data. 
     // We'll just skip content-length in those cases. 
     if (! $this->_zlib_oc && ! $CFG->item('compress_output')) 
     { 
      header('Content-Length: ' . strlen($output)); 
     } 

     if (config_item('rest_enable_logging')) 
     { 
      $this->_log_response($output); 
     } 

     exit($output); 
    }  
} 

我希望代碼是很清楚,但我將重點放在關鍵領域:

  1. 添加類字段$_log_id;
  2. 修飾以插入存儲日誌
  3. 添加方法_log_response()的ID來更新日誌記錄與響應
  4. 的方法
  5. 方法_log_response()稱爲內response()

瞧!