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"}
The getVar() method will pull from $_REQUEST, so will return any data from $_GET, $POST, or $_COOKIE. While this is convenient, you will often need to use a more specific method, like: $request->getGet() $request->getPost() $request->getServer()
Determining Request Type This can be checked with the isAJAX() and isCLI() methods: <? php // Check for AJAX request. if ($request->isAJAX()) { // ... } // Check for CLI Request if ($request->isCLI()) { // ... }
This is the correct way to do it.
$input_data = json_decode(trim(file_get_contents('php://input')), true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With