I'm am using the Slim Framework Version 3 and have some problems.
我使用的是Slim框架版本3,并且有一些问题。
$app-> post('/', function($request, $response){
$parsedBody = $request->getParsedBody()['email'];
var_dump($parsedBody);
});
result is always:
结果总是:
null
零
Can you help me ?
你能帮我吗?
2 个解决方案
#1
0
It depends how you are sending data to the route. This is a POST route, so it will expect the body data to standard form format (application/x-www-form-urlencoded
) by default.
这取决于您如何将数据发送到路由。这是一个POST路径,因此默认情况下,它将期望body数据到标准表单格式(应用程序/x-www-form-urlencode)。
If you are sending JSON to this route, then you need to set the Content-type
header to application/json
. i.e. the curl would look like:
如果将JSON发送到此路由,则需要将Content-type头设置为application/ JSON。即旋度是这样的
curl -X POST -H "Content-Type: application/json" \
-d '{"email": "a@example.com"}' http://localhost/
Also, you should validate that the array key you are looking for is there:
此外,您应该验证所查找的数组键是否存在:
$parsedBody = $request->getParsedBody()
$email = $parsedBody['email'] ?? false;
#2
0
Please, try this way:
请试试这个方法:
$app-> post('/yourFunctionName', function() use ($app) {
$parameters = json_decode($app->request()->getBody(), TRUE);
$email = $parameters['email'];
var_dump($email);
});
I hope this helps you!
我希望这对你有帮助!
#1
0
It depends how you are sending data to the route. This is a POST route, so it will expect the body data to standard form format (application/x-www-form-urlencoded
) by default.
这取决于您如何将数据发送到路由。这是一个POST路径,因此默认情况下,它将期望body数据到标准表单格式(应用程序/x-www-form-urlencode)。
If you are sending JSON to this route, then you need to set the Content-type
header to application/json
. i.e. the curl would look like:
如果将JSON发送到此路由,则需要将Content-type头设置为application/ JSON。即旋度是这样的
curl -X POST -H "Content-Type: application/json" \
-d '{"email": "a@example.com"}' http://localhost/
Also, you should validate that the array key you are looking for is there:
此外,您应该验证所查找的数组键是否存在:
$parsedBody = $request->getParsedBody()
$email = $parsedBody['email'] ?? false;
#2
0
Please, try this way:
请试试这个方法:
$app-> post('/yourFunctionName', function() use ($app) {
$parameters = json_decode($app->request()->getBody(), TRUE);
$email = $parameters['email'];
var_dump($email);
});
I hope this helps you!
我希望这对你有帮助!