【问题标题】:How do I correctly handle a PHP post request?如何正确处理 PHP 发布请求?
【发布时间】:2020-06-21 10:11:41
【问题描述】:

我正在制作一个小型 API 来处理一些调查。

我有以下身体:

{
  "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'];

但我收到以下错误:

注意:未定义索引:名称在 C:\xampp\htdocs\LimeAPI\api\objects\create.php上线 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) "" }

我做错了什么?

提前谢谢你

【问题讨论】:

    标签: php post httprequest


    【解决方案1】:

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

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

    那么,因为这返回一个字符串,你需要解码JSON:

    $content = json_decode($post_body);
    

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

    echo $content->name
    

    【讨论】:

      【解决方案2】:

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

      当在请求中使用application/x-www-form-urlencodedmultipart/form-data 作为HTTP Content-Type时,通过HTTP POST 方法传递给当前脚本的变量关联数组。

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

      【讨论】: