如果我发送一个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
}
Run Code Online (Sandbox Code Playgroud)
发布的数据看起来像这样:
{
"sessionkey":"somecrazykey",
"longitude":"-111.12345",
"latitude":"33.12345",
"reqtype":"checkin",
"location":"the mall",
"public":"true"
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个建议在核心的代码片段,请参阅https://trac.cakephp.org/ticket/6125.也许这就是你要找的东西.
- 比约恩
小智 5
您可以使用以下最简单的方法:
$data = $this->request->input ( 'json_decode', true) ;
Run Code Online (Sandbox Code Playgroud)