19

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"}
0

12 Answers 12

30

This is the correct way to do it.

$input_data = json_decode(trim(file_get_contents('php://input')), true);
Sign up to request clarification or add additional context in comments.

3 Comments

php://input can only be read once, and the CodeIgniter input class will already have read it by this point.
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.
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.
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.

Comments

10

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

Comments

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()

1 Comment

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.
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

Comments

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

1 Comment

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, and also Explaining entirely code-based answers
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')

Comments

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

Comments

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']);

Comments

0

Just add correct content type to your request header

Content-Type: application/json

Comments

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...(

Comments

-1

try

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

OR

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

print_r($tmp);

1 Comment

$this->input->post() array is always empty. I cannot figure out why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.