PHP SLIM - 如何在正文中接收 json 格式?

PHP SLIM - how to receive a json format in the body?

我不知道如何接收正文中发送到我的 API 的 JSON。

我有这行代码。但是没用。

$app->post('/api/respuestas',function($request) use ($app){
    $json= $request ->getParsedBody();
     $datos= json_decode($json);

   echo "$datos";  // HERE IS THE PROBLEM  NOTHING HAPPENS


   //create sql
     $sql = "
              // sql insert into
          ";


    //execute  sql

    // try {
    //     $db= new db();
    //     $db = $db->connect();
    //
    //     $stmt = $db->query($sql);
    //     $preguntas = $stmt->fetchAll(PDO::FETCH_OBJ);
    //     $db = null;
    //     echo json_encode($preguntas);
    // } catch (PDOException $e) {
    //   echo '{"error":{"text":'.$e->getMessage().'}}';
    // }

});

如何知道我是否真的得到 json?

**新**我正在使用 EasyRest 进行测试

会不会是打错了?

$json = $request->gerParsedBody(); ^

用 getParsedBody 替换 gerParsedBody 应该可以。

好的,我找到了解决方案

代码

$app->post('/api/respuestas', function($request) {
  $data = $request->getParsedBody();
  $name = $data['product'];

    echo" hello $name" ;

});

POST 测试

source