之前记得写过这2者的区别,今天看群里有个朋友也提出了怪异的问题,说是“file_get_contents('php://input')获取不到curl post请求的数据的问题”?
其实这并不是所谓的"怪异",理解2者的区别其实就明白原因啦,好,直接举个例子吧,2个文件:
1:发送数据的文件,如下:

<?php
$ch = curl_init();
$data = ['username' => '周伯通', 'password' => '123456','sign'=>'asdfg123456'];
$url = 'http://xxx.com/fpost.php';//fpost.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
$res = file_get_contents('php://input');
var_dump('file_get_contents 数据是:'.$res);
echo'<br> post 数据是:';
var_dump($_POST);

运行后我们会发现:file_get_contents('php://input')不能接收curl post过来的数组。

解释:
如果POST的原始数据是一维数组或&拼接的标准格式的键值对字符串,那么可以用$_POST来获取。
如果要通过file_get_contents获取,这种情况下可以发送json字符串,用json_encode转一下,或者使用http_build_query:比如上面修改如下:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//POST数据

结果:

string() "file_get_contents 数据是:username=%E5%91%A8%E4%BC%AF%E9%80%9A&password=123456&sign=asdfg123456"
post 数据是:array() { ["username"]=> string(9) "周伯通" ["password"]=> string(6) "123456" ["sign"]=> string(11) "asdfg123456" }

所以大家在使用中,注意下传参的方式即可解决问题。无非就这2种比较常用的数据传参方式啦。

file_get_contents('php://input') 和POST的区别的更多相关文章

  1. $POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别

    $POST .$HTTP_RAW_POST_DATA.php://input三者之间的区别 访问原始 POST 数据的更好方法是 php://input或者$HTTP_RAW_POST_DATA.$H ...

  2. button与input[type=”button”]的区别

    button与input[type="button"]的区别 特别感谢 @守护晴天 ,指出了博客中不细致不严谨的地方,也让我学到了更多,更多是觉得抱歉,由于自己的不细致可能误导了一 ...

  3. **对比$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')

    最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下. public function __construct($token, $wxuser = ''){ $ ...

  4. (转载)file_get_contents("php://input")

    (转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...

  5. file_get_contents("php://input")的使用方法

    $data = file_get_contents("php://input"); //input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 php: ...

  6. file_get_contents("php://input")的用法

    $data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 ...

  7. 举例说明$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【转】file_get_contents("php://input")的使用方法

    $data = file_get_contents("php://input");    php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好 ...

  9. 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别

    这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...

随机推荐

  1. ubuntu18.04新体验

    虽然ubuntu18.04LST版本早出来了,但自己原来的ubuntu16.04还可以用,就懒得折腾了. 但最近ubuntu崩了,就想尝尝鲜...结果发现还挺好用的,准确地说,ubuntu是越来越好用 ...

  2. CROS+node-basis+ajax

    $.ajax({ url: this.baseUrl + this.restful.showDesigerViewList, type: "post", dataType: &qu ...

  3. Vue 学习笔记 — 无法避免的dom操作

    简书 使用Vue了一段时间,感觉确实不错,"数据驱动视图"非常好用,大部分情况下都不需要关心dom,但是凡事都有例外,总有一些时候我们必须要直接对dom进行操作,比如下面这个例子: ...

  4. [Swift]LeetCode156.二叉树的上下颠倒 $ Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  5. [Swift]LeetCode7. 反转整数 | Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  6. [Swift]LeetCode40. 组合总和 II | Combination Sum II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  8. [Swift]LeetCode868. 二进制间距 | Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  9. Jedis与Luttuce区别

    如果你在网上搜索Redis 的Java客户端,你会发现,大多数文献介绍的都是 Jedis. 不可否认,Jedis是一个优秀的基于Java语言的Redis客户端. 但是,其不足也很明显:Jedis在实现 ...

  10. Java基础--Eclipse使用

    Eclipse是JavaWeb开发最常用的工具,下面详细介绍一下如何下载安装最新版Eclipse.本文eclipse的使用是在JDK安装配置完成的基础上进行的,否则Eclipse安装后可能无法运行程序 ...