file_get_contents('php://input') 和POST的区别
之前记得写过这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的区别的更多相关文章
- $POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别
$POST .$HTTP_RAW_POST_DATA.php://input三者之间的区别 访问原始 POST 数据的更好方法是 php://input或者$HTTP_RAW_POST_DATA.$H ...
- button与input[type=”button”]的区别
button与input[type="button"]的区别 特别感谢 @守护晴天 ,指出了博客中不细致不严谨的地方,也让我学到了更多,更多是觉得抱歉,由于自己的不细致可能误导了一 ...
- **对比$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')
最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下. public function __construct($token, $wxuser = ''){ $ ...
- (转载)file_get_contents("php://input")
(转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...
- file_get_contents("php://input")的使用方法
$data = file_get_contents("php://input"); //input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 php: ...
- file_get_contents("php://input")的用法
$data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 ...
- 举例说明$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】file_get_contents("php://input")的使用方法
$data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好 ...
- 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别
这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...
随机推荐
- [LeetCode] Smallest Subtree with all the Deepest Nodes 包含最深结点的最小子树
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- android sdk 历史版本下载地址
https://developer.android.google.cn/studio/archive#android-studio-3-0?utm_source=androiddevtools& ...
- JavaEE开发之Spring中的依赖注入与AOP编程
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- JQuery实现 图片上传
用到的文件,我都已经打包好了,自行下载: https://files.cnblogs.com/files/lguow/lib.rar 核心代码如下: <input type="hidd ...
- input type=passoord 密码框的明密文(显示和隐藏) 显示
最近在写一个新的项目,从头开始写,所以就要从注册登录开始做起.以前写登录注册模块的时候,无外乎给input框一个type=”password”就可以了,近期因为要涉及到显示隐藏状态的切换. 样式代码如 ...
- React-Native的基本控件属性方法,对React-Native的学习,从熟悉基本控件开始。
对React-Native的学习,从熟悉基本控件开始. View 属性方法 序号 名称 属性Or方法 类型 说明 1 accessibilityLabel 属性 string 2 accessib ...
- React Native组件、生命周期及属性传值props详解
创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...
- 【安富莱二代示波器教程】第17章 附件B---功能扩展和改进方向
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=45785 第17章 附件B---功能扩展和改进方向 ...
- [Swift]LeetCode252.会议室 $ Meeting Rooms
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [Swift]LeetCode438. 找到字符串中所有字母异位词 | Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...