Home  >  Article  >  Backend Development  >  Solving the problem that the put request in CodeIgniter RestServer cannot obtain parameters_PHP tutorial

Solving the problem that the put request in CodeIgniter RestServer cannot obtain parameters_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:421056browse

Solving the problem that the put request in CodeIgniter RestServer cannot obtain parameters

I recently encountered a painful problem when using restserver. I found that the parameters obtained by $this->put are all null. After checking, I found that this seems to be a common problem, see the link: https://github.com/chriskacerguis/codeigniter-restserver/issues/362

Let’s take a look at the official explanation first: see 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.

That is, this function can read the put parameters carried when accessed through cURL, or the parameters in http headers. However, after testing, even if the parameters are placed in headers, $this->put() cannot be accessed. The root cause may be that it is blocked somewhere in the source code. Zajia has not yet found a fundamental solution, but the following two methods can temporarily solve the problem:

1, consistent with post, still passing parameters in the body. Write a function in the base class:

    public function getPut($key){
        return $this->input->input_stream($key);
    }
When the client accesses, just pass the parameters in the body normally and it will be ok. Note that the parameters cannot be obtained through $this->post() at this time, and must be obtained from input_stream. The above function supports fetching multiple fields at the same time, such as:
$data = $this->getPut(array('tel', 'name', 'addr'));
In fact, all input functions in CI should support fetching multiple fields at the same time, but Restserver's this->get() post() does not support it.

Supplement: When putting parameters in the body, you can directly use $this->put() to get the corresponding fields. The document says it is in the headers, but it is actually in the body! However, $this->put() does not support multiple fields, so the above function is still meaningful.

$this->delete() also has this problem. The parameters in the headers cannot be read, but the parameters in the body can be read! ! !

2. The parameters are passed in the header and a function is written in the base class:

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

I personally recommend the first method, the parameters are passed in the body! It's best if you follow http rules, and don't use them badly in the header.

ps: The problem that put in restserver cannot obtain parameters has nothing to do with the setting of Content-Type: application/json.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088531.htmlTechArticleSolution to the problem that the put request in CodeIgniter RestServer cannot obtain parameters. Recently, I encountered a painful problem with restserver. I found that The parameters obtained by $this-put are all null. I checked and found that this appearance...
Statement:
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