如何处理作为 HTTP Post 发送到 cakephp 应用程序的 json 数据? [英] How do I handle json data sent as an HTTP Post to a cakephp app?
本文介绍了如何处理作为 HTTP Post 发送到 cakephp 应用程序的 json 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我收到一个 HTTP Post,其中 http 请求的正文只是一个 UTF8 编码的字符串,我如何在我的 cakephp 控制器中访问该数据?看起来 $this->params 只包含以下内容:
<代码>{经过":[],命名":[],"控制器":"用户","action":"签到",插件":空,网址":{"ext":"json","url":"用户/checkin.json"},形式":[],"isAjax":false}
发布的数据如下所示:
<代码>{"sessionkey":"somecrazykey","经度":"-111.12345","纬度":"33.12345","reqtype":"签到","location":"商场",公开":真实"}
解决方案
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) 和 $jsonData !== false) {$this->data = $jsonData;}}
这是一个被提议放在核心的代码片段,见https://trac.cakephp.org/ticket/6125.也许这就是您要找的.p>
-- 比约恩
If I'm being sent an HTTP Post where the body of the http request is just a UTF8 encoded string, how do I access that data in my cakephp controller? It appears that $this->params only contains the following:
{
"pass":[],
"named":[],
"controller":"users",
"action":"checkin",
"plugin":null,
"url":{
"ext":"json",
"url":"users/checkin.json"
},
"form":[],
"isAjax":false
}
The data being posted looks something like this:
{
"sessionkey":"somecrazykey",
"longitude":"-111.12345",
"latitude":"33.12345",
"reqtype":"checkin",
"location":"the mall",
"public":"true"
}
解决方案
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;
}
}
This is a codesnippet which was proposed to be in the core, see https://trac.cakephp.org/ticket/6125. Maybe it's what you're looking for.
-- Bjorn
这篇关于如何处理作为 HTTP Post 发送到 cakephp 应用程序的 json 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文