CodeIgniter RestServer中put请求获取不到参数的问题解决_PHP教程

WBOY
Release: 2016-07-12 09:01:42
Original
1011 people have browsed it

CodeIgniter RestServer中put请求获取不到参数的问题解决

最近用restserver遇到个蛋疼的问题,发现$this->put得到的参数都是null。查了一下发现,这貌似这个普遍问题,参见链接:https://github.com/chriskacerguis/codeigniter-restserver/issues/362

还是先来看下官方的解释:参见 http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814

$this->put()

Reads in PUT arguments set in the HTTP headers or via cURL.
Copy after login

即该函数可以读取通过cURL访问时携带的put参数,或者在http headers里的参数。但是经过测试,即便参数放headers里,$this->put()也访问不到,其根本原因可能是在源码上某个地方给屏蔽了。杂家暂时也没找到解决的根本方法,但以下两种可以暂时解决问题:

1,与post保持一致,仍然在body里传参数。在基类里写个函数:

    public function getPut($key){
        return $this->input->input_stream($key);
    }
Copy after login
客户端访问的时候正常传参数在body里,就ok了。注意此时通过$this->post()是得不到参数的,必须从input_stream里取。上述函数支持多字段同时取,如:
$data = $this->getPut(array('tel', 'name', 'addr'));
Copy after login
其实CI里从input出来的函数应该都支持多字段同时取,但Restserver的this->get() post()却不支持。

补充:当把参数放body里时,直接用$this->put()就可以获得到对应字段了,文档说是在headers,实际是在body里!但$this->put()不支持多字段,故上述函数还是有意义的。

$this->delete()也有这个问题,读不到headers里的参数,但能读到body里的!!!

2,参数在header里传,基类里写个函数:

    /**
     * 获得key对应的header
     * @param $key
     * @return mixed
     */
    public function getHeader($key){
        return $this->input->get_request_header($key, TRUE);
    }
Copy after login

个人推荐第一种哈,参数在body里传!能按照http规矩来最好,header里不要烂用。

ps:restserver里put获得不到参数的问题跟Content-Type:application/json 这个设置无关。
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088531.htmlTechArticleCodeIgniter RestServer中put请求获取不到参数的问题解决 最近用restserver遇到个蛋疼的问题,发现$this-put得到的参数都是null。查了一下发现,这貌...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!