我正在尝试使用linux curl命令(Lamp Server)将json数据发布到php文件
$ curl -V -H "Content-Type: application/json" -X POST -d '{"id": "123"}'
http://localhost/crm/UpdateUser.php
在UpdateUser.php中,
<?php echo var_dump ($_POST);?>
输出:
[ec2-user@ip-10-35-1-181 ~]$ curl -v -H "Content-Type: application/json" -X POST -d '{"id": "123"}' http://viacrm.odema.net/crm/UpdateUser.php
* Hostname was NOT found in DNS cache
* Trying 54.217.206.217...
> POST /crm/UpdateUser.php HTTP/1.1
> User-Agent: curl/7.36.0
> Host: 192.168.1.16
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 200 OK
< Date: Mon, 16 Jun 2014 12:25:00 GMT
* Server Apache/2.2.27 (Amazon) is not blacklisted
< Server: Apache/2.2.27 (Amazon)
< X-Powered-By: PHP/5.3.28
< Content-Length: 13
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(0) {
}
* Closing connection 0
始终将Post数据显示为空,我甚至尝试使用&#34; ACCEPT:application / json&#34;,仍然是同样的问题。请任何人都可以指导这个吗?
答案 0 :(得分:2)
$_POST
仅包含解码application/x-www-form-urlencoded
请求的结果。您需要阅读原始请求正文。如果您启用了always_populate_raw_post_data
配置指令,则原始正文将位于$HTTP_RAW_POST_DATA
;否则你可以通过阅读php://input
流来获取它。
答案 1 :(得分:0)
而不是$_POST
尝试:
<?php
print($HTTP_RAW_POST_DATA);
?>