[英]How to post data of 'content-type application/json' to slim php rest service
i am having issue for posting data of mime type content-type application/json
from Chrome Advance rest Client to slim framework web service. 我在将来自Chrome Advance rest Client的mime类型
content-type application/json
数据发布到苗条框架Web服务时遇到问题。 I tried these codes to send in application/json
我尝试了这些代码以发送
application/json
$app->post('/register', function() use ($app) {
$app->add(new \Slim\Middleware\ContentTypes());
$params = $app->request->getBody();
$name = $params->name;
$email = $params->email;
$password = $params->password;
...});
tried this also 也尝试了这个
$params = json_decode($app->request()->getBody());
var_dumb($params); //get NULL value here
Getting errors of 得到错误
Trying to get property of non-object to this `$name = $params->name;`
Please help me how to catch application/json format of data? 请帮我如何捕获application / json格式的数据? Thank you
谢谢
As per the above details, assuming your raw JSON looks something like this 根据上面的详细信息,假设您的原始JSON看起来像这样
{"name":"John Smith", "mail":"jhon@mail.com", "password":"foobar"}
You can access your params array like this 您可以像这样访问您的params 数组
$app->post('/register', function () use ($app) {
$params = $app->request->getBody() ;
$params = array_filter($params);
if(!empty($params)){
$name = $params['name'];
$mail = $params['mail'];
$pass = $params['password'];
// print $name;
}
})->name("register");
or if you are posting in Advanced Rest client via Content-Type: application/x-www-form-urlencoded
you can use $app->request->post();
或者,如果您通过
Content-Type: application/x-www-form-urlencoded
在Advanced Rest客户端中发布Content-Type: application/x-www-form-urlencoded
,则可以使用$app->request->post();
to access your array 访问您的数组
$app->post('/register/', function () use ($app) {
$userInfo = $app->request()->params() ;
//or
$userInfo = $app->request->post() ;
$name = $userInfo['name'];
$mail = $userInfo['email'];
$pass = $userInfo['password'];
// print $name
})->name("register");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.