【问题标题】:Codeigniter rest controller post response as xml formatCodeigniter 休息控制器以 xml 格式发布响应
【发布时间】:2018-01-19 13:36:12
【问题描述】:

我正在使用 Codeigniter REST_Controller 进行 API 调用, 当我尝试使用格式(json / xml / HTML)进行调用时,它的工作原理

但是当我尝试进行 post 调用时,我需要 XML 数据,但它总是无法获取 XML 数据。它总是只显示 JSON 数据。这是我的问题的屏幕截图

我的控制器是

<?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());
    }
}

【问题讨论】:

  • 您必须在屏幕截图中传递 format=xml 以显示 html
  • 检查docs。您的网址应类似于 http://localhost/resume/user?format=xml
  • 是的!我也试过了,还是不行
  • 我需要一个 POST 请求的解决方案,我可以发送像“localhost/resume/user?format=xml”这样的数据,即使它不起作用

标签: php json xml codeigniter


【解决方案1】:

REST_Controller 使用服务器变量 CONTENT_TYPE 在库中使用以下方法检测格式类型:

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 请求并使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2012-01-25
    • 2011-09-21
    相关资源
    最近更新 更多