18

I am trying to receive and parse a JSON object sent in a POST request using Codeigniter but I cannot "find" it.

This is my controller code:

public function parse () {

  $json = $this->input->post();
  $json = stripslashes($json);
  $json = json_decode($json);

  print_r($json);

}

This is my JSON object:

{"data":"value"}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

12 Answers12

30

This is the correct way to do it.

$input_data = json_decode(trim(file_get_contents('php://input')), true);
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
  • php://input can only be read once, and the CodeIgniter input class will already have read it by this point. – Chris Cox May 01 '16 at 13:44
  • Just add correct content type to your request header, 'Content-Type: application/json'. As mentioned by Chris, Codeigniter reads input before your method get called. – Shailesh May 03 '16 at 15:49
  • 2
    Whatever you guys are commenting, they have no relation to the date at which I posted this answer, which was correct at the date I posted. – CMCDragonkai May 03 '16 at 16:19
12
$post = json_decode($this->security->xss_clean($this->input->raw_input_stream));

When you use $this->input->raw_input_stream you can read it multiple times and its basically the same as file_get_contents('php://input'). This works on CI3. I don't know if it works on CI2.

Firze
  • 3,939
  • 6
  • 48
  • 61
9

Try this code, it will output an array with all your parameters.

$this->input->raw_input_stream;

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

$input_data will return array

Ismael Moral
  • 722
  • 1
  • 9
  • 35
Tan Trinh
  • 173
  • 2
  • 4
2

Firze's answer is correct but here is a more elaborated answer. I am not allowed to comment so I am posting it as an answer.

It has to do with CodeIgniter not being able to fetch JSON. jQuery does some under the hood tricks and transforms your data into form-data-x, that's why it works when you don't specify the content type, don't encode your object, or other situations.

If you want a pure JSON the solution is to use $this->input->raw_input_stream to fetch your JSON and decode it using php's json_decode. Check the full answer and code below:

Retrieve JSON POST data in CodeIgniter

Community
  • 1
  • 1
jimasun
  • 604
  • 9
  • 12
2

Try this instead

$json = $this->input->post('data');
$json = stripslashes($json);
$json = json_decode($json);
print_r($json);

You need to pass in the key of the data variable you want from the post array as an argument to post()

aziz punjani
  • 25,586
  • 9
  • 47
  • 56
  • This works: data={"color":"blue"} but is that ok? Would be cool with a library that could "convert" them so that I could use the Codeigniter validation. – Jonathan Clark Dec 22 '11 at 17:45
1
controller:
puplic function exam(){
$obj = file_get_contents('php://input');
$edata = json_decode($obj);
echo $edata->name;
}
Go to post man->type->post
url:http://www.exam.com/exam
formate:json
{
"name":"atm fahim"
}
==>send
ATM Fahim
  • 31
  • 3
  • 2
    Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), and also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) – Anh Pham Dec 18 '18 at 11:25
0

Are you sure you're POSTing the data and not doing a GET instead? I ran into this issue earlier today (which is how I found this question) and I was doing a POST but using JSONP which seems to be done with a GET.

CodeIgniter has a function called get_post that will get the data from wherever it happens to be.

$this->input->get_post_string('data'); 

I hope this helps you out.

You can do it manually like so if you'd like.

function get_post($index = '', $xss_clean = FALSE){
    if ( ! isset($_POST[$index]) )
    {
        return $this->get($index, $xss_clean);
    }
    else
    {
        return $this->post($index, $xss_clean);
    }
}
Chris M
  • 121
  • 1
  • 7
0

I know this is an old post, but for others looking, this might be helpful:

On the browser side, I create my data packet using code similar to this pattern:

    var form_data = { };
    $.each($('#mvt_dialog_form').serializeArray(), function() {
        form_data[this.name] = this.value;
    }); 

   // add the address data to the payload
   var result = { 
        form_data: form_data,
        locations: addressData,
        selected_location:  selectedLocation
    };

   // now wrap it all up with a pretty bow
   // Seriously, the key:value format is required for codeigniter INPUT class to be able to "see"
   var movement = {
       movement_dlg: JSON.stringify(result)
   };

I then "post" movement to the server. In the controller, I then use the following logic:

    // Perform XSS filtering
    $postData = $this->input->post(NULL, TRUE);
    $result = json_decode($postData['movement_dlg']);
300baud
  • 540
  • 4
  • 17
0

Just add correct content type to your request header

Content-Type: application/json
Shailesh
  • 490
  • 5
  • 11
0

In order to use the standard CI methods. In index.php, insert a couple of lines:

    $json = json_decode(trim(file_get_contents('php://input')), true);
    if(!empty($json)) {
        $_POST = $json;
    }

Either implement in the bootstrap. RIP Codigniter...(

Leonid Rakuts
  • 51
  • 1
  • 2
0

make sure you have POST data, using $this->input->post() it will always return empty, you should put on the input type name $this->input->post('name_of_input_text')

Khairu Aqsara
  • 1,321
  • 3
  • 14
  • 27
-1

try

json_decode(array($this->input->post()))

OR

$tmp[] = (array)json_decode($this->input->post());

print_r($tmp);
Philip
  • 4,592
  • 2
  • 20
  • 28