我正在使用 curl 发送:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{firstname:james}" http://hostname/index.php
我正在尝试在 index.php 中显示这样的 POST
<?php
die(var_dump($_POST));
?>
哪些输出
array(0) {
}
我一定是对通过 POST 发送 JSON 数据有误解
谢谢你的时间
最佳答案
$_POST
是一个数组,只有当您以 URL 编码格式发送 POST 正文时才会填充它。 PHP 本身不会自动解析 JSON,因此不会填充 $_POST
数组。您需要获取原始 POST 主体并自行解码 JSON:
$json = file_get_contents('php://input');
$values = json_decode($json, true);
关于php - curl JSON 时 POST 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990767/