2017-08-11 61 views
1

我使用笨REST_Controller製作API調用, 當我試圖獲得與格式調用(JSON/XML/HTML),它的工作笨休息控制器後響應爲XML格式

但是當我試着做後調用,在這裏我需要XML數據作爲結果,但它總是無法獲取XML數據。它總是隻顯示JSON數據。我在這裏分享我的問題

enter image description here

我的控制器的屏幕截圖是使用服務器變量CONTENT_TYPE庫使用下面的方法來檢測格式類型

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
require(APPPATH.'libraries/REST_Controller.php'); 

class User extends REST_Controller { 

    public function index_post() 
    { 
     $this->response($this->post()); 
    } 
} 
+0

你必須在屏幕截圖中傳遞format = xml它顯示html – BSB

+0

檢查[docs](https://github.com/chriskacerguis/codeigniter-restserver#content-types)。你的URL應該看起來像'http:// localhost/resume/user?format = xml'。 – Tpojka

+0

是啊!我也試過了,它不起作用 –

回答

0

REST_Controlleris:

protected function _detect_input_format() 
    { 
     // Get the CONTENT-TYPE value from the SERVER variable 
     $content_type = $this->input->server('CONTENT_TYPE'); 
     if (empty($content_type) === FALSE) 
     { 
      // If a semi-colon exists in the string, then explode by ; and get the value of where 
      // the current array pointer resides. This will generally be the first element of the array 
      $content_type = (strpos($content_type, ';') !== FALSE ? current(explode(';', $content_type)) : $content_type); 
      // Check all formats against the CONTENT-TYPE header 
      foreach ($this->_supported_formats as $type => $mime) 
      { 
       // $type = format e.g. csv 
       // $mime = mime type e.g. application/csv 
       // If both the mime types match, then return the format 
       if ($content_type === $mime) 
       { 
        return $type; 
       } 
      } 
     } 

你可以在POST請求和使用情況下編寫自己的方法。