POST empty when curling JSON

前端 未结 2 720
死守一世寂寞
死守一世寂寞 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条回答
  •  萌比男神i
    2021-02-07 20:04

    $_POST only works if you are sending encoded form data. You are sending JSON, so PHP cannot parse it into the $_POST array.

    You need to read directly from the POST body.

    $post = fopen('php://input', r);
    $data = json_decode(stream_get_contents($post));
    fclose($post);
    

提交回复
热议问题