# file\_get\_contents('php://input')获取不到Curl POST的数据

**URL:** https://www.shiqidu.com/t/topic/231
**Category:** 问题求助
**Tags:** 问与答
**Created:** [2017 年10 月 1 日 04:55 UTC](https://www.shiqidu.com/t/topic/231 "2017-10-01T04:55:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### 作者： ![xiangcaodashu](https://www.shiqidu.com/user_avatar/www.shiqidu.com/xiangcaodashu/32/479_2.png) [@xiangcaodashu](https://www.shiqidu.com/u/xiangcaodashu)
#### 发布日期： [2017 年10 月 1 日 04:55 UTC](https://www.shiqidu.com/t/topic/231/1 "2017-10-01T04:55:55Z")

</div>

今天遇到了一个file\_get\_contents(‘php://input’)获取不到curl post请求的数据的问题。  
代码如下：  
两个php脚本  
1.php

```php
$ch = curl_init();    
    
$data = json_encode(array('username' => 'aaa', 'password' => 'xxx','sign'=>'dasddsadxf123dsad'));    
    
$url = 'http://test.com/test_php/file_get_contents_post/2.php';    
$ch = curl_init(); //初始化curl    
curl_setopt($ch, CURLOPT_URL, $url);//设置链接    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息    
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//POST数据    
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");    
$response = curl_exec($ch);//接收返回信息    
if(curl_errno($ch)){//出错则显示错误信息    
    print curl_error($ch);    
}    
curl_close($ch); //关闭curl链接    
echo $response;//显示返回信息    

```

2.php

```php
$res = file_get_contents('php://input');    
var_dump($res);    

```

返回结果为空，在1.php里打印返回结果，获取不到2.php返回的内容。请问是什么原因？有人知道吗？

---

<div class="post-metadata">

### 作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)
#### 发布日期： [2017 年10 月 1 日 04:57 UTC](https://www.shiqidu.com/t/topic/231/2 "2017-10-01T04:57:23Z")

</div>

`file_get_contents('php://input')`不能接收curl post过来的数组。  
如果POST的原始数据是一维数组或&拼接的标准格式的键值对字符串，用`$_POST`来获取。  
这种情况下你可以发送json字符串，用`json_encode`转一下，然后在 **2.php** 就能接收到啦。

---

<div class="post-metadata">

### 作者： ![system](https://sqdstatic.shiqidu.com/original/2X/4/4e5d23b7a075007594c841a3f6ea84c9ceb18168.svg) [@system](https://www.shiqidu.com/u/system)
#### 发布日期： [2026 年7 月 23 日 08:16 UTC](https://www.shiqidu.com/t/topic/231/3 "2026-07-23T08:16:03Z")

</div>

此话题已在最后回复的 60 分钟后被自动关闭。不再允许新回复。
