之前记得写过这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. [LeetCode] Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

  2. 解决Django+Vue前后端分离的跨域问题及关闭csrf验证

      前后端分离难免要接触到跨域问题,跨域的相关知识请参:跨域问题,解决之道   在Django和Vue前后端分离的时候也会遇到跨域的问题,因为刚刚接触Django还不太了解,今天花了好长的时间,查阅了 ...

  3. Java和js操作json

    Js中 Json字符串转json对象 //将json格式的字符串转为json对象 var t = JSON.parse('{"name":123}'); alert(t.name) ...

  4. Fence Repair POJ - 3253 (贪心)

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  5. koa2学习(一)

    前期准备: node环境 npm包管理工具 安装Koa npm install --save koa 第一个程序 创建index.js const Koa = require('koa'); cons ...

  6. Ubuntu添加中文输入法

    1.点击左上角Dash home 2.输入keyboard input methods 3.点击keyboard input methods,选择input methods,打钩. 4.点击下三角,选 ...

  7. [转]jenkins2 插件安装

    文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd Jenkins的安装包和插件在7个国家有20多个镜 ...

  8. 实战深度学习(下)OpenCV库

    在上一节中,我们讲到了OpenCV库的安装,现在我们来进行实战,看如何利用Python来调用OpenCV库. 一: 如果您的电脑是win10的系统,那么请您按下win键,再按下空格键,输入Python ...

  9. [Swift]LeetCode154. 寻找旋转排序数组中的最小值 II | Find Minimum in Rotated Sorted Array II

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  10. [Swift]LeetCode468. 验证IP地址 | Validate IP Address

    Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither ...