php - curl JSON 时 POST 为空

标签 php json post curl

我正在使用 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/

相关文章:

Android上传服务在json数组中上传图片

javascript - jQuery 函数未提交 Ajax 请求

c# - 使用 Flurl 发布 `multipart/form-data`

php - 使用 PHP 从 html 网页生成 jpg 或 png

php - 如何在 Laravel 中运行 Python 脚本?

php - 单击 "show"时显示详细信息

php - 'reportes.id_usuario' 中的未知列 'on clause'

javascript - 如何访问 JavaScript 事件回调函数中的变量?

javascript - 循环 JSON 并在满足条件时停止。然后数一下物体的数量

javascript - $.post() 的第三个参数不起作用