如果我发送了一个HTTP Post,其中http请求的主体只是一个UTF8编码的字符串,我如何在我的cakephp控制器中访问该数据?看来$ this-> params只包含以下内容:
{
"pass":[],
"named":[],
"controller":"users",
"action":"checkin",
"plugin":null,
"url":{
"ext":"json",
"url":"users\/checkin.json"
},
"form":[],
"isAjax":false
}
发布的数据如下所示:
{
"sessionkey":"somecrazykey",
"longitude":"-111.12345",
"latitude":"33.12345",
"reqtype":"checkin",
"location":"the mall",
"public":"true"
}
答案 0 :(得分:7)
if($ this-> RequestHandler-> requestedWith('json')){ if(function_exists('json_decode')){ $ jsonData = json_decode(utf8_encode(trim(file_get_contents('php:// input'))),true); }
if(!is_null($jsonData) and $jsonData !== false) {
$this->data = $jsonData;
}
}
这是一个建议在核心的代码片段,请参阅https://trac.cakephp.org/ticket/6125。也许这就是你要找的东西。
- Bjorn
答案 1 :(得分:3)
您可以使用以下最简单的方法:
$data = $this->request->input ( 'json_decode', true) ;