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

$data = file_get_contents(“php://input”);
  php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令。 而且,这样的情况下 $HTTP_RAW_POST_DATA 默认没有填充, 比激活 always_populate_raw_post_data 潜在需要更少的内存。 enctype=“multipart/form-data” 的时候 php://input 是无效的。

1.php://input 可以读取http entity body中指定长度的值,由Content-Length指定长度,不管是POST方式或者GET方法提交过来的数据。但是,一般GET方法提交数据 时,http request entity body部分都为空。

2.php://input 与$HTTP_RAW_POST_DATA读取的数据是一样的,都只读取Content-Type不为multipart/form-data的数据。

3,Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST

4,PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA

  1. 只有Coentent-Type为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。

6.只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。

7.php://input数据总是跟HTTPRAWPOSTDATA相同,但是php://input比HTTP_RAW_POST_DATA相同,但是php://input比HTTPR​AWP​OSTD​ATA相同,但是php://input比HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini

8.PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。

例子
1.php用file_get_contents(“php://input”)或者$HTTP_RAW_POST_DATA可以接收xml数据
比如:
getXML.php;//接收XML地址

1 <?php
2 $xmldata = file_get_contents("php://input");
3 $data = (array)simplexml_load_string($xmldata);
4 ?>

这里的$data就是包含xml数据的数组,具体php解析xml数据更新详细的方法
sendXML.php
复制代码

 1 <?php2 $xml = '<xml>xmldata</xml>';//要发送的xml3 $url = 'http://localhost/test/getXML.php';//接收XML地址4 $header = 'Content-type: text/xml';//定义content-type为xml5 $ch = curl_init(); //初始化6 curl curl_setopt($ch, CURLOPT_URL, $url);//设置链接7 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息8 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头9 curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
10 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST数据
11 $response = curl_exec($ch);//接收返回信息
12
13 if(curl_errno($ch)){//出错则显示错误信息
14     print curl_error($ch);
15 }
16
17 curl_close($ch); //关闭curl链接
18 echo $response;//显示返回信息
19 ?>

2.一个手机上传图片到服务器的小程序
上传文件
复制代码

1 <?php2 //@file phpinput_post.php3 $data=file_get_contents('btn.png');4 $http_entity_body = $data;5 $http_entity_type = 'application/x-www-form-urlencoded';6 $http_entity_length = strlen($http_entity_body);7 $host = '127.0.0.1';8 $port = 80;9 $path = '/image.php';
10 $fp = fsockopen($host, $port, $error_no, $error_desc, 30);
11 if ($fp){
12     fputs($fp, "POST {$path} HTTP/1.1\r\n");
13     fputs($fp, "Host: {$host}\r\n");
14     fputs($fp, "Content-Type: {$http_entity_type}\r\n");
15     fputs($fp, "Content-Length: {$http_entity_length}\r\n");
16     fputs($fp, "Connection: close\r\n\r\n");
17     fputs($fp, $http_entity_body . "\r\n\r\n");
18
19     while (!feof($fp)) {
20         $d .= fgets($fp, 4096);
21     }
22     fclose($fp);
23     echo $d;
24 }
25 ?>

接收文件

1 <?php2 /**3 *Recieve image data4 **/error_reporting(E_ALL);5 function get_contents() {6     $xmlstr= file_get_contents("php://input");7     $filename=time().'.png';8     if(file_put_contents($filename,$xmlstr)){9         echo 'success';
10     }else{
11         echo 'failed';
12     }
13 }
14 get_contents();
15 ?>

3.获取HTTP请求原文

 1 /**2 * 获取HTTP请求原文3 * @return string4 */5 function get_http_raw() {6     $raw = ''; // (1) 请求行 7     $raw .= $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n";8    // (2) 请求Headers9    foreach($_SERVER as $key => $value) {
10       if(substr($key, 0, 5) === 'HTTP_') {
11           $key = substr($key, 5);
12           $key = str_replace('_', '-', $key);
13           $raw .= $key.': '.$value."\r\n";
14       }
15    }
16    // (3) 空行
17    $raw .= "\r\n";
18    // (4) 请求Body
19    $raw .= file_get_contents('php://input');
20     return $raw;
21 }

file_get_contents相关推荐

  1. php中file_get_contents如何读取大容量文件

    php中file_get_contents如何读取大容量文件 一.总结 一句话总结:使用file_get_contents()进行分段读取,file_get_contents()函数可以分段读取 1. ...

  2. ob_get_contents();basename;file_get_contents用法

    ob_get_contents(); ob_end_clean(); ob_start() 使用ob_start()把输出那同输出到缓冲区,而不是到浏览器. 然后用ob_get_contents得到缓 ...

  3. php 如何得到 file_get_contents 数据,如何使用file_get_contents在PHP中发布数据?

    使用 file_get_contents发送HTTP POST请求不是那么难,实际上:正如你猜到的,你必须使用$ context参数. PHP手册中有一个例子,在这个页面:HTTP context o ...

  4. php file抓取不到内容,深入file_get_contents函数抓取内容失败的原因分析

    用file_get_contents来抓取页面内容不成功,可能是因为有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程we ...

  5. php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...

  6. curl重写php file_get_contents

    file_get_contents在连接不上的时候会提示Connection refused,有时候会带来不便:另外,curl的性能比file_get_contents高,所以用curl重写file_ ...

  7. php下curl与file_get_contents性能对比

    为什么80%的码农都做不了架构师?>>>    上一篇讲了 <php使用curl替代file_get_contents>, 后续贴出了curl和file_get_cont ...

  8. php file_get_contents 临时目录 fopen,php fopen()和file_get_contents() 区别介绍

    php中读取文件可以使用fopen和file_get_contents这两个函数,二者之间没有本质区别,只是前者读取文件的php代码相比后者要复杂一点.本文章通过实例向大家讲解fopen和file_g ...

  9. php 读写文件 file_put_contents() 与 file_get_contents() 函数用法

    file_put_contents file_put_contents() 函数用于把字符串写入文件,成功返回写入到文件内数据的字节数,失败则返回 FALSE. 语法: int file_put_co ...

  10. 简评file_get_contents与curl 效率及稳定性

    做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效: $config['con ...

最新文章

  1. 7、在对象内部尽量直接访问实例变量
  2. 影响线型缩聚物分子量的因素_【CRPS】通过硼烷引发剂实现氧气引发的超高分子量聚合物的可控合成...
  3. html绘制头像原样教程,CSS实例教程:创意CSS3头像展示教程
  4. 机器学习实战之K近邻算法
  5. 华为P30真机屏幕盖板曝光:超窄边框水滴屏
  6. 【Mac】一些软件的图片和视频位置 QQ 微信
  7. 【Siddhi】Siddhi的window操作
  8. PTA — 单词长度 (15 分)
  9. python3 numpy二维方法_Python numpy:基于坐标创建二维值数组
  10. python实操案例_python实战案例分享 - 讲师
  11. 对于长沙互联网发展,一个外来两年Java程序员的所见所感所愿
  12. Mac安装Etcd,配置Etcd可视化页面Etcd Browser
  13. 网课必备·几款特色各异的pdf软件大全
  14. 百度云:当AI遇到视频
  15. blgo ? 博客 !
  16. mysql的WAL技术是什么_我们常听到的WAL到底是什么
  17. 2021.01.05丨根据基因名称拼接表达量与相关注释
  18. 第一次作为面试官,我很紧张
  19. FileAlterationMonitor监听目录下文件变化
  20. Python数据分析初学之分析表格

热门文章

  1. UE4:转换成VR项目
  2. [Echarts][全网唯一]X轴axisLabel随文字长度自动旋转的实现思路
  3. CMS内容管理系统(含小程序,Uni APP) 搭建
  4. VMvare虚拟机删除快照时卡住的解决办法
  5. spring retry, guava retrying 的整合-sisyphus java 重试框架
  6. 《嵌入式开发》实验项目
  7. 拟合系数 / 决定系数 / R方 / R^2的理解
  8. three.js 法线贴图
  9. 拒绝iap cracker
  10. HDU 5869 Different GCD Subarray Query (数学gcd+树状数组离线查询)