我试图接收和解析使用Codeigniter在POST请求中发送的JSON对象,但我无法"找到"它.
这是我的控制器代码:
public function parse () {
$json = $this->input->post();
$json = stripslashes($json);
$json = json_decode($json);
print_r($json);
}
Run Code Online (Sandbox Code Playgroud)
这是我的JSON对象:
{"data":"value"}
Run Code Online (Sandbox Code Playgroud)
CMC*_*kai 26
这是正确的方法.
$input_data = json_decode(trim(file_get_contents('php://input')), true);
Run Code Online (Sandbox Code Playgroud)
$post = json_decode($this->security->xss_clean($this->input->raw_input_stream));
Run Code Online (Sandbox Code Playgroud)
当你使用$ this-> input-> raw_input_stream时,你可以多次读取它,它与file_get_contents('php:// input')基本相同.这适用于CI3.我不知道它是否适用于CI2.
小智 6
试试这个代码,它将输出一个包含所有参数的数组。
$this->input->raw_input_stream;
$input_data = json_decode($this->input->raw_input_stream, true);
Run Code Online (Sandbox Code Playgroud)
$input_data
将返回数组
试试这个
$json = $this->input->post('data');
$json = stripslashes($json);
$json = json_decode($json);
print_r($json);
Run Code Online (Sandbox Code Playgroud)
您需要从 post 数组中传入所需数据变量的键作为参数post()
归档时间: |
|
查看次数: |
37189 次 |
最近记录: |