zoukankan      html  css  js  c++  java
  • php获取输入流

    uc中的用到的代码(在api/uc.php)代码:

    $post = xml_unserialize(file_get_contents('php://input'));

    php手册(http://cn.php.net/manual/zh/wrappers.php.php)说明:

    php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

    Note: A stream opened with php://input can only be read once; the stream does not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.

    理解

    1.符号的理解:与符号”http://“对比。可以理解php://表示这是一种协议。不同的是它是php自己定义与使用的协议。

    输入域名的时候,htt://后面输入的是域的名字。那么php://后面的"input"也可以表示一个文件。是一个php已经定义好的文件。比如有ouput。php自己知道去哪里找这个文件。

    2.相关联知识: 常量$HTTP_RAW_POST_DATA是获取post提交的原始数据(contains the raw POST data)。php://input 的所完成的功能与它一样:都是包含post过来的原始数据。

    使用php://input比$HTTP_RAW_POST_DATA更好。因为:使用php://inpu不受到配置文件的影响。常量$HTTP_RAW_POST_DATA受到php.ini配置的影响。在php.ini中有个选项,always_populate_raw_post_data = On。该配置决定是否生成常量$HTTP_RAW_POST_DATA(保存post数据)。

    php://input的方式与$HTTP_RAW_POST_DATA数据都同样不适用于表单提交的"multipart/form-data"类型的数据。所以,两种的区别就是在于是否受到配置文件的影响。另外,内存消耗更少,这个怎么理解?

    3.怎么得到其内容:使用file_get_contents('php://input')得到输入流的内容。php://input的结果只读。

     

    4.使用规则:必须是post方式提交的数据。而且提交的不能是multipart/form-data类型的数据(当需要上传文件,就会在form标签中添加属性enctype="multipart/form-data")

    想更好地理解它是怎么使用的。做个试验就明白:

    html文件输入内容:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>

    <form action="input.php" method="post">


    <input name="username" type="text" />


    <input name="password" type="text" />
    <input name="name" type="submit"value="提交数据" />
    </form>

    </body>
    </html>

    input.php:

    <?php

    $input = file_get_contents('php://input');
    var_dump($input);exit;
    ?>

    提交后,得到结果:

    string(97) "username=%E5%90%8D%E5%AD%97&password=%E5%AF%86%E7%A0%81&name=%E6%8F%90%E4%BA%A4%E6%95%B0%E6%8D%AE"

    接下来改为get方式提交,结果为:

    string(0) ""

    说明没有任何数据


  • 相关阅读:
    R--相关分布函数、统计函数的使用
    Spark Streaming
    统计与分布的相关知识
    Python--WebDriverWait+expected_conditions的一个应用
    Python+Selenium与Chrome如何进行完美结合
    Python+Selenium+Chrome 的一个案例
    python -使用Requests库完成Post表单操作
    JetBrains PyCharm 2018.2.4 x64 工具里如何安装bs4
    用JetBrains PyCharm 开发工具写一个简单python案例
    dom4j学习总结(一)
  • 原文地址:https://www.cnblogs.com/wangtao_20/p/1955666.html
Copyright © 2011-2022 走看看