Post JSON to Codeigniter controller

2019-03-18 11:11发布

问题:

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

回答1:

This is the correct way to do it.

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


回答2:

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



回答3:

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



回答4:

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



回答5:

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



回答6:

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



回答7:

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


回答8:

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


回答9:

Just add correct content type to your request header

Content-Type: application/json


回答10:

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



回答11:

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


回答12:

try

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

OR

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

print_r($tmp);