php 如何在 Slim 中访问 POST 请求的 JSON 请求正文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28073480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a JSON request body of a POST request in Slim?
提问by guillermoandrae
I'm just a newbie in the Slim framework. I've written one API using Slim framework.
我只是 Slim 框架的新手。我已经使用 Slim 框架编写了一个 API。
A POST request is coming to this API from an iPhone app. This POST request is in JSON format.
一个 POST 请求从一个 iPhone 应用程序来到这个 API。此 POST 请求采用 JSON 格式。
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.
但我无法访问在 iPhone 请求中发送的 POST 参数。当我尝试打印 POST 参数的值时,每个参数都得到“null”。
$allPostVars = $application->request->post(); //Always I get null
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:
然后我尝试获取即将到来的请求的正文,将正文转换为 JSON 格式并将其作为响应发送回 iPhone。然后我得到了参数的值,但它们的格式非常奇怪,如下所示:
"{\"password\":\"admin123\",\"login\":\"[email protected]\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"
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 请求参数来到这个 API 文件。尽管它们在 中不可访问$application->request->post()
,但它们正在进入请求正文。
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?
我的第一个问题是我应该如何从请求正文访问这些 POST 参数,我的第二个问题是为什么在将请求正文转换为 JSON 格式后,请求数据显示为上述奇怪的格式?
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;
?>
回答by guillermoandrae
Generally speaking, you can access the POST
parameters individually in one of two ways:
一般来说,您可以通过以下POST
两种方式之一单独访问参数:
$paramValue = $application->request->params('paramName');
or
或者
$paramValue = $application->request->post('paramName');
More info is available in the documentation: http://docs.slimframework.com/#Request-Variables
文档中提供了更多信息:http: //docs.slimframework.com/#Request-Variables
When JSON is sent in a POST
, you have to access the information from the request body, for example:
当 JSON 在 a 中发送时POST
,您必须从请求正文中访问信息,例如:
$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
});
回答by lurker123456
"Slim can parse JSON, XML, and URL-encoded data out of the box" - http://www.slimframework.com/docs/objects/request.htmlunder "The Request Body".
“Slim 可以开箱即用地解析 JSON、XML 和 URL 编码的数据” - http://www.slimframework.com/docs/objects/request.html在“请求正文”下。
Easiest way to handle a request in any body form is via the "getParsedBody()". This will do guillermoandrae example but on 1 line instead of 2.
处理任何正文形式的请求的最简单方法是通过“getParsedBody()”。这将执行 guillermoandrae 示例,但在 1 行而不是 2 行。
Example:
例子:
$allPostVars = $application->request->getParsedBody();
Then you can access any parameters by their key in the array given.
然后您可以通过给定数组中的键访问任何参数。
$someVariable = $allPostVars['someVariable'];