How can I get http request body data in codeigniter?

I was able to get this working by using

$jsonArray = json_decode(file_get_contents('php://input'),true); 

The above will read the request body from the input request and then decodes the JSON to an associative array.

I would still be interested in refactoring this code if CI has a wrapper for reading input stream http body data as I am above. Fairly new to this framework.


Try to use $this->input->raw_input_stream


The "request" object mentioned in that post belongs to a package named "codeigniter-restserver". You can find more info here.

The accepted answer is a working one, but CodeIgniter does provide $this->input->raw_input_stream that gives you exactly what you need. So to write things the CodeIgniter way, you should use:

$jsonArray = json_decode($this->input->raw_input_stream, true);

And you'll notice that the source code of raw_input_stream also uses the file_get_contents('php://input') method.

Tags:

Codeigniter