我有一個在 docker 中運行的 Nginx 和 PHP 容器。當我運行以下請求時,$_POST 為空。我使用 Phpstorm 選項“在 PHP 腳本的第一行中斷”進行除錯。
curl -X POST 'localhost/api/v1/customers' -H 'Content-Type: application/json' --data-raw '{"id":"12345"}'
什么會導致 $_POST 為空?Nginx 中是否需要配置?
當我嘗試使用表單資料請求時,它可以作業,但我需要它與 JSON 一起作業。
curl -X POST 'localhost/api/v1/customers' -H 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'id=12345'
uj5u.com熱心網友回復:
發布application/json資料時,$_POST始終為空,您可以使用 PHP 獲取 JSON 請求正文:
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
$ curl -X POST 'localhost/api/v1/customers' \
-H 'Content-Type: application/json' \
-d '{"id":"12345"}'
Array
(
[id] => 12345
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/392471.html
上一篇:代碼空間:強制python版本
