POST empty when curling JSON

前端 未结 2 678
死守一世寂寞
死守一世寂寞 2021-02-07 19:41

I\'m using curl to send this:

curl -i -H \"Accept: application/json\" -H \"Content-type: application/json\" -X POST -d \"{firstname:james}\" http://hostname/inde         


        
2条回答
  •  生来不讨喜
    2021-02-07 19:58

    $_POST is an array that is only populated if you send the POST body in URL encoded format. PHP does not parse JSON by itself automatically and hence does not populate the $_POST array. You need to get the raw POST body and decode the JSON yourself:

    $json = file_get_contents('php://input');
    $values = json_decode($json, true);
    

提交回复
热议问题