【发布时间】:2015-03-20 08:12:36
【问题描述】:
我只是 Slim 框架的新手。我使用 Slim 框架编写了一个 API。
一个 POST 请求从 iPhone 应用程序发送到此 API。此 POST 请求采用 JSON 格式。
但我无法访问从 iPhone 发出的请求中发送的 POST 参数。当我尝试打印 POST 参数的值时,每个参数都得到“null”。
$allPostVars = $application->request->post(); //Always I get null
然后我尝试获取即将到来的请求的正文,将正文转换为 JSON 格式并将其作为响应发送回 iPhone。然后我得到了参数的值,但它们的格式很奇怪,如下所示:
"{\"password\":\"admin123\",\"login\":\"admin@gmail.com\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"
所以可以肯定的一件事是 POST 请求参数将进入此 API 文件。尽管在$application->request->post() 中无法访问它们,但它们正在进入请求正文。
我的第一个问题是我应该如何从请求正文中访问这些 POST 参数,我的第二个问题是为什么在将请求正文转换为 JSON 格式后,请求数据会显示为上述奇怪的格式?
以下是必要的代码sn -p:
<?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;
?>
【问题讨论】:
标签: php json post http-post slim