cakephp - 如何处理以HTTP Post形式发送到cakephp应用程序的json数据?

标签 cakephp

如果正在向我发送HTTP请求,而其中HTTP请求的主体只是一个UTF8编码的字符串,如何在我的cakephp Controller 中访问该数据?看来$ 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"
}

最佳答案

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。也许这就是您要寻找的。

-比约恩

关于cakephp - 如何处理以HTTP Post形式发送到cakephp应用程序的json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1766621/

相关文章:

php - CakePHP 命名约定/文件结构

CakePHP:语法错误,意外的 T_LIST,期待 T_STRING

php - 在我看来,代码是否太多了? CakePHP/MVC设计模式

php - 从 ssl 移动到非 ssl 时 session 不保存

php - 面包屑未添加到缓存 View 中

php - 按关联模型与 HasMany 关系 CakePHP 3 排序

CakePHP 是否可以将动态数据传递给元素以供整个站点使用?

php - CakePHP Auth 允许 JSON 扩展

php - 使用 Gmail 使用 TLS 从 PHP 传出 SMTP

php - CakePHP JavaScript 文件中的国际化