简体   繁体   English

如何将'content-type application / json'的数据发布到苗条的php rest服务

[英]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.

相关问题 Slim - 如何使用“Content-Type:application / json”标题发送响应? - Slim - How to send response with “Content-Type: application/json” header? 如何将 Slim 中的 Content-Type 设置为 XML? - How to set Content-Type in Slim to XML? curl -X POST -H'Content-Type:application / json' -d to PHP - curl -X POST -H 'Content-Type: application/json' -d to PHP 如果标题中包含“ Content-Type:application / json”,则不会创建PHP 5.4.9 $ _POST超全局变量 - PHP 5.4.9 $_POST superglobal not created if 'Content-Type: application/json' is in header 内容类型application / json不返回POST字段 - Content-Type application/json doesn't return POST fields 为什么是header('Content-Type:application / json'); 在我们的网络服务中? - Why header('Content-Type: application/json'); in our web service? 未记录的PHP自动将content-type:application / json解码为$ _REQUEST - PHP undocumented automatically decode content-type:application/json to $_REQUEST 何时在PHP中使用标题('Content-Type:application / json') - When to use header('Content-Type: application/json') in PHP PHP API内容类型:application / json。 响应为空数据 - PHP API Content-Type: application/json. Response empty data PHP header('Content-Type: application/json'); 不管用 - PHP header('Content-Type: application/json'); is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM