Pan*_*ate 10 php post json curl
我用curl发送这个:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{firstname:james}" http://hostname/index.php
Run Code Online (Sandbox Code Playgroud)
我正试图在index.php中显示这样的POST
<?php
die(var_dump($_POST));
?>
Run Code Online (Sandbox Code Playgroud)
哪个输出
array(0) {
}
Run Code Online (Sandbox Code Playgroud)
我必须误解一些关于通过POST发送JSON数据的事情
感谢您的时间
dec*_*eze 29
$_POST是一个只有在以URL编码格式发送POST正文时才会填充的数组.PHP不会自动解析JSON,因此不会填充$_POST数组.您需要获取原始POST主体并自行解码JSON:
$json = file_get_contents('php://input');
$values = json_decode($json, true);
Run Code Online (Sandbox Code Playgroud)
$_POST仅在您发送编码的表单数据时才有效.您正在发送JSON,因此PHP无法将其解析为$_POST数组.
您需要直接从POST正文中读取.
$post = fopen('php://input', r);
$data = json_decode(stream_get_contents($post));
fclose($post);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7185 次 |
| 最近记录: |