如何正确处理 PHP post 请求?

How do I correctly handle a PHP post request?

我正在做一个小的 API 来处理一些调查。

我有以下 body:

{
  "name":"1: asd",
  
  "children":[
     {
        "name":"2: are",
        "children":[
           {
              "name":"3: wat wat",
              "children":[
                 {
                    "name":"4: in da hut",
                    "context":{
                       "question":"in da hut",
                       "questionType":"rbText",
                       "answers":[
                          {
                             "value":"",
                             "index":0,
                             "indexValue":1
                          }
                       ]
                    }
                 },
                 {
                    "name":"5: k k k k",
                    "context":{
                       "question":"k k k k",
                       "questionType":"rbText",
                       "answers":[
                          {
                             "value":"",
                             "index":0,
                             "indexValue":1
                          }
                       ]
                    }
                 }
              ],
              "context":{
                 "question":"wat wat",
                 "questionType":"rbMultiple",
                 "answers":[
                    {
                       "value":"sim",
                       "index":2,
                       "indexValue":4
                    },
                    {
                       "value":"nao",
                       "index":3,
                       "indexValue":5
                    }
                 ]
              }
           }
        ],
        "context":{
           "question":"are",
           "questionType":"rbMultiple",
           "answers":[
              {
                 "value":"potatoes",
                 "index":4,
                 "indexValue":3
              },
              {
                 "value":"nay",
                 "index":4,
                 "indexValue":3
              }
           ]
        }
     }
  ],
  "context":{
     "question":"asd",
     "questionType":"rbText",
     "answers":[
        {
           "value":"",
           "index":5,
           "indexValue":2
        }
     ]
  }

}

在 php 方面,为了测试请求是否成功,我尝试了以下操作:

echo $_POST['name'];

但是我得到以下错误:

Notice: Undefined index: name in C:\xampp\htdocs\LimeAPI\api\objects\create.php on line 15

所以我添加了

var_dump($_POST)

在数组中正确打印我的请求。

所以我更改了我的代码以遍历数组并打印元素:

foreach($_POST as $item) {    
    echo $item;
    var_dump($item);
}

但现在我得到以下信息:

<b>Notice</b>:  Array to string conversion in <b>C:\xampp\htdocs\LimeAPI\api\objects\create.php</b> on line <b>17</b><br /> Arrayarray(1) {   ["
         {
            "name":"2: are",
            "children":[
               {
                  "name":"3: wat wat",
                  "children":[
                     {
                        "name":"4: in da hut",
                        "context":{
                           "question":"in da hut",
                           "questionType":"rbText",
                           "answers":[
                              {
                                 "value":"",
                                 "index":0,
                                 "indexValue":1
                              }
                           "]=>   string(0) "" }

我做错了什么?

提前致谢

您首先需要访问整个请求体:

$post_body = file_get_contents("php://input");

那么,因为这个returns是一个字符串,所以需要解码JSON:

$content = json_decode($post_body);

然后您将有一个表示请求正文的对象,可以使用箭头运算符 name 检索该对象:

echo $content->name

来自 $_POST documentation(强调我的):

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

任何其他 MIME 类型(application/jsonapplication/xml...)不会自动解码,需要您自己解析。