【问题标题】:JSON Post data empty - Linux curl CommandJSON Post 数据为空 - Linux curl 命令
【发布时间】:2014-08-06 06:47:20
【问题描述】:

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

帖子数据总是显示为空,我什至尝试使用“接受:应用程序/json”,仍然是同样的问题。请问有人可以指导吗?

【问题讨论】:

标签: json linux curl


【解决方案1】:

$_POST 仅包含解码application/x-www-form-urlencoded 请求的结果。您需要阅读原始请求正文。如果您打开了always_populate_raw_post_data 配置指令,那么原始正文将在$HTTP_RAW_POST_DATA 中;否则,您可以通过从php://input 流中读取来获取它。

【讨论】:

    【解决方案2】:

    代替$_POST试试看:

    <?php
        print($HTTP_RAW_POST_DATA);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2015-01-02
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多