如何在Slim中访问POST请求的JSON请求主体? [英] How to access a JSON request body of a POST request in Slim?

查看:658
本文介绍了如何在Slim中访问POST请求的JSON请求主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是Slim框架中的新手。我使用Slim框架编写了一个API。

I'm just a newbie in the Slim framework. I've written one API using Slim framework.

从iPhone应用程序发出此API请求。此POST请求采用JSON格式。

A POST request is coming to this API from an iPhone app. This POST request is in JSON format.

但是我无法访问iPhone请求中发送的POST参数。当我尝试打印POST参数的值时,每个参数都得到null。

But I'm not able to access the POST parameters that are sent in a request from iPhone. When I tried to print the POST parameters' values I got "null" for every parameter.

$allPostVars = $application->request->post(); //Always I get null

然后我试图得到一个即将到来的请求的身体,转换身体转换为JSON格式并将其作为对iPhone的响应发回。然后我得到了参数的值,但它们的格式非常奇怪,如下所示:

Then I tried to get the body of a coming request, convert the body into JSON format and sent it back as a response to the iPhone. Then I got the parameters' values but they are in very weird format as follows:

"{\"password\":\"admin123\",\"login\":\"admin@gmail.com\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"

所以有一点可以肯定的是POST请求参数来到这个API文件。虽然在 $ application-> request-> post()中无法访问它们,但它们正在进入请求正文。

So one thing for sure is POST request parameters are coming to this API file. Though they are not accessible in $application->request->post(), they are coming into request body.

我的第一个问题是如何从请求主体访问这些POST参数,我的第二个问题是为什么请求数据在转换后会显示为如上所述的奇怪格式请求体是否为JSON格式?

My first issue is how should I access these POST parameters from request body and my second issue is why the request data is getting displayed into such a weird format as above after converting the request body into JSON format?

以下是必要的代码片段:

Following is the necessary code snippet:

<?php

    require 'Slim/Slim.php';    

    \Slim\Slim::registerAutoloader();

    //Instantiate Slim class in order to get a reference for the object.
    $application = new \Slim\Slim();

    $body = $application->request->getBody();
    header("Content-Type: application/json");//setting header before sending the JSON response back to the iPhone
    echo json_encode($new_body);// Converting the request body into JSON format and sending it as a response back to the iPhone. After execution of this step I'm getting the above weird format data as a response on iPhone.
    die;
?>


推荐答案

一般来说,你可以访问 POST 参数有两种方式:

Generally speaking, you can access the POST parameters individually in one of two ways:

$paramValue = $application->request->params('paramName');

$paramValue = $application->request->post('paramName');

文档中提供了更多信息: http://docs.slimframework.com/#Request-Variables

More info is available in the documentation: http://docs.slimframework.com/#Request-Variables

发送JSON时在 POST 中,您必须访问请求正文中的信息,例如:

When JSON is sent in a POST, you have to access the information from the request body, for example:

$app->post('/some/path', function () use ($app) {
    $json = $app->request->getBody();
    $data = json_decode($json, true); // parse the JSON into an assoc. array
    // do other tasks
});

这篇关于如何在Slim中访问POST请求的JSON请求主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆