Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 卷曲JSON时为空_Php_Json_Post_Curl - Fatal编程技术网

Php 卷曲JSON时为空

Php 卷曲JSON时为空,php,json,post,curl,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中这样显示帖子 <?php die(var_dump($_POST)); ?> 我一定是误解了通过POST发送JSON数据 感谢您抽出时间$\u POST是一

我正在使用curl发送以下内容:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{firstname:james}" http://hostname/index.php
我正试图在index.php中这样显示帖子

<?php
die(var_dump($_POST)); 
?>
我一定是误解了通过POST发送JSON数据

感谢您抽出时间

$\u POST
是一个数组,只有在您以URL编码格式发送帖子正文时才会填充该数组。PHP不会自动解析JSON,因此不会填充
$\u POST
数组。您需要获得原始的帖子正文并自己解码JSON:

$json = file_get_contents('php://input');
$values = json_decode($json, true);

$\u POST
仅在发送编码表单数据时有效。您正在发送JSON,因此PHP无法将其解析到
$\u POST
数组中

你需要直接从文章正文中阅读

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

我同意:您没有正确设置JSON/HTTP post数据。看看这个链接:我认为你需要引用变量,因为它们不是字符串numbers@Waygood事实上,这也是事实。JSON无效。结果应该是“{”firstname:“james”}”
$post = fopen('php://input', r);
$data = json_decode(stream_get_contents($post));
fclose($post);