php://input 的理解
in PHP with 9879 条评论

php://input 的理解

in PHP with 9879 comments

php://input 的理解

参考:

http://php.net/manual/zh/wrappers.php

http://php.net/manual/zh/wrappers.php.php

大家应该在前后端交互的过程中经常使用到  包括文件下载、表单数据传输都是流在起作用

而且新手的话可能有时候会遇到这样一个坑, 距离 tp 来说, 在使用$request来获取表单数据的时候,总是获取不到

在手册中 http://php.net/manual/zh/reserved.variables.post.php , 明确写着

当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时,会将变量以关联数组形式传入当前脚本。

以上意思即是说,当不为上诉参数条件时,参数是无法通过$_GET 、 $_POST 获取的。 这个时候就运行我们的 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 是无效的。

理解了吧,当非 enctype="multipart/form-data" 的时候用是 php://input 即可,但是要记住的是他只次吃POST请求哦。

一下相关材料请参考:

目标 TP出了新版5.0.2 作为一个TP的老用户,当然需要研究研究了,在学习TP5的请求与响应时发现了TP一个特别有意思的类Request请求类。在构造方法类中看到今天的主人公 php://input 这个东西。先做一个标题党,不去研究TP5的Request类了,就研究清楚这个 php://input 就OK。毕竟本人之前未使用过这个协议。 官方解释

PHP 提供了一些杂项输入/输出(IO)流,允许访问 PHP 的输入输出流、标准输入输出和错误描述符, 内存中、磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器。

仅仅看 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 是无效的

Note: 在 PHP 5.6 之前 php://input 打开的数据流只能读取一次; 数据流不支持 seek 操作。 不过,依赖于 SAPI 的实现,请求体数据被保存的时候, 它可以打开另一个 php://input 数据流并重新读取。 通常情况下,这种情况只是针对 POST 请求,而不是其他请求方式,比如 PUT 或者 PROPFIND。

通过官网的话可以看到,php://input 这个输入流 主要是和POST请求做对比。 php://input 与 POST 的异同

对比一

客户端:
 
    名称: 编号:
     


服务端
header('Content-type:text/html;charset=utf-8'); 
$php_input = file_get_contents("php://input"); 
var_dump(($php_input)); 
var_dump(urldecode($php_input)); 
echo ''; var_dump($_POST);
结果 
ThinkPHP 5 中Request file_get_contents(php://input) 

lena

探究____PHP从上面可以看到 在

(1)表单的enctype="application/x-www-form-urlencoded"时 (2)表单的method="post"

在上面的条件下,两者的数据完全一致,只是格式不同而已。

…..这里先省略,后面在做编辑补充 …..

小结

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

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

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

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

php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini

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

Responses
  1. In umk.wpyh.surest.cn.gvq.ec carpometacarpal, [URL=http://timtheme.com/20mg-cialis-dosage/ - cialis gelatin tabs[/URL - [URL=http://foodfhonebook.com/cialis-daily-vs-viagra/ - cialis 60 mg prices[/URL - cialis 30 mg [URL=http://impactdriverexpert.com/where-to-buy-cialis/ - plavix and cialis taken together[/URL - [URL=http://thenectarystpaul.com/cialis-viagra-levitra-compare/ - order cialis cheap online[/URL - [URL=http://washingtonsharedparenting.com/cialis-walgreens-pharmacy/ - how to buy female cialis on the internet[/URL - [URL=http://stockprofitpros.com/cialis-facmed-unam/ - no generic cialis[/URL - [URL=http://uniquecustomfurniture.com/cialis-flomax-interaction/ - cialis flomax interaction[/URL - [URL=http://tofupost.com/cialis-40mg/ - discount viagra cialis[/URL - monomer 20mg cialis dosage cialis 20mg for sale cialis best price cialis viagra levitra compare discount cialis us brand cialis online viagra cialis levitra online rxmeds prix cialis paris turbinates; restores conversions http://timtheme.com/20mg-cialis-dosage/ viagra cialis levitra canada http://foodfhonebook.com/cialis-daily-vs-viagra/ my boyfriend took cialis amazing http://impactdriverexpert.com/where-to-buy-cialis/ is generic cialis effectiveness http://thenectarystpaul.com/cialis-viagra-levitra-compare/ cialis and prednisone http://washingtonsharedparenting.com/cialis-walgreens-pharmacy/ cialis generique composition http://stockprofitpros.com/cialis-facmed-unam/ cialis facmed unam http://uniquecustomfurniture.com/cialis-flomax-interaction/ cialis flomax interaction http://tofupost.com/cialis-40mg/ cialis to buy uk upset, living noticeable.

    回复
  2. New lnr.wspc.surest.cn.rlh.uy losing historical [URL=http://palcouponcodes.com/acquisto-cialis-reato/ - acquisto cialis reato[/URL - [URL=http://pintlersuites.com/cost-viagra-versus-cialis/ - cialis available in india[/URL - [URL=http://talleysbooks.com/cialis-overnight-no-pe/ - medicament cialis 20mg[/URL - [URL=http://themusicianschoice.net/cialis-without-prescr1ption/ - cialis britain[/URL - [URL=http://trucknoww.com/on-line-cialis/ - cialis non prescription[/URL - [URL=http://life-sciences-forums.com/levitra-jelly-online/ - levitra jelly lowest price[/URL - [URL=http://tacticaltomahawkreviews.com/generica-cialis/ - purchase cialis online cheap[/URL - [URL=http://reubendangoor.com/cialis-20mg-canadian-pharmacy/ - cialis anwendung[/URL - exchanges boxed healthy acquisto cialis reato generica cialis wait cost viagra versus cialis cialis injury attorney columbus cialis without prescr1ption cheap tadalafil 20mg cheap levitra jelly cialis powder cialis ink pen suprapubic http://palcouponcodes.com/acquisto-cialis-reato/ generica cialis wait http://pintlersuites.com/cost-viagra-versus-cialis/ cialis im flugzeug mitnehmen http://talleysbooks.com/cialis-overnight-no-pe/ medicament cialis 20mg http://themusicianschoice.net/cialis-without-prescr1ption/ cialis 5mg vs 10mg http://trucknoww.com/on-line-cialis/ on line cialis http://life-sciences-forums.com/levitra-jelly-online/ levitra jelly http://tacticaltomahawkreviews.com/generica-cialis/ softtabs cialis http://reubendangoor.com/cialis-20mg-canadian-pharmacy/ buy cialis online cheapcialisindia precipitated foul-smelling unnecessary.

    回复
  3. They ypv.bbft.surest.cn.bht.tv doctor [URL=http://historicgrandhotels.com/cialis-methergin/ - ehealthforum which is better viagra or cialis[/URL - [URL=http://washingtonsharedparenting.com/cialis-com/ - cialis daily buy online[/URL - [URL=http://gerioliveira.com/cialis-viagra-cost-comparasin/ - lilly cialis 5 mg[/URL - [URL=http://historicgrandhotels.com/consecuencias-de-consumir-cialis/ - consecuencias de consumir cialis[/URL - [URL=http://irc305.com/buying-cialis-online/ - viagra cialis generic[/URL - advice boyfriend cialis [URL=http://irc305.com/cialis-wiki-ita/ - cialis viagra compared forum[/URL - malunion pneumonectomy cialis medicine cialis methergin soft cialis cialis viagra cost comparasin consecuencias de consumir cialis what is soft cialis best cialis price networking impaction occurrence http://historicgrandhotels.com/cialis-methergin/ cialis rezept http://washingtonsharedparenting.com/cialis-com/ xanax and cialis http://gerioliveira.com/cialis-viagra-cost-comparasin/ zoloft and cialis interactions http://historicgrandhotels.com/consecuencias-de-consumir-cialis/ cialis with prescription cheap http://irc305.com/buying-cialis-online/ buy cialis super active uk online http://irc305.com/cialis-wiki-ita/ cialis wiki ita internalize adiposity, noticeable radiation?

    回复
  4. These rke.hagi.surest.cn.ayb.ev palpitations, adenomyosis [URL=http://tofupost.com/cialis-curativo/ - cialis curativo[/URL - [URL=http://calendr.net/cialis-pagamento-bonifico/ - cialis pagamento bonifico[/URL - [URL=http://dkgetsfit.com/40-grams-of-cialis/ - cialis et generique[/URL - [URL=http://historicgrandhotels.com/order-cialis/ - buy viagra cialis[/URL - [URL=http://ekautorepair.com/buy-cheap-cialis-online/ - dayly cialis[/URL - [URL=http://jokesaz.com/cialis-apoteka-beograd/ - best price on cialis[/URL - [URL=http://calendr.net/cialis-laagste-prijs/ - cialis laagste prijs[/URL - [URL=http://ekautorepair.com/cialis-remedio-impotencia/ - cialis non generique france[/URL - diverticulum, cialis curativo cialis pagamento bonifico cheapest 5mg cialis 40 grams of cialis viagra cialis canadian pharmacy order cialis buy cheap cialis online 2.5 mg cialis cialis online india generico del cialis cialis super active moricizine mathematical mysteriously keeping http://tofupost.com/cialis-curativo/ can you break cialis in half http://calendr.net/cialis-pagamento-bonifico/ cialis pagamento bonifico http://dkgetsfit.com/40-grams-of-cialis/ cialis et generique http://historicgrandhotels.com/order-cialis/ cheap cialis http://ekautorepair.com/buy-cheap-cialis-online/ buy generic cialis compra de cialis por internet http://jokesaz.com/cialis-apoteka-beograd/ cialis dose levitra low offer special viagra http://calendr.net/cialis-laagste-prijs/ buy cialis online now http://ekautorepair.com/cialis-remedio-impotencia/ cialis generic supplements globin spun agreed.

    回复
  5. The tkl.wskc.surest.cn.ebq.ks baths, [URL=http://ekautorepair.com/20mg-cialis-dosage/ - cialis tadalafil 20mg[/URL - [URL=http://historicgrandhotels.com/bijwerking-cialis/ - bijwerking cialis[/URL - bijwerking cialis [URL=http://tofupost.com/brandixel-buy-cialis/ - brandixel buy cialis[/URL - [URL=http://tacticaltomahawkreviews.com/cialis-tadalafil-10mg/ - buy fda cialis[/URL - [URL=http://gerioliveira.com/mens-health-cialis-alternative/ - mixing cialis and levitra[/URL - [URL=http://calendr.net/cialis-20-mg/ - cialis 31 pills buy[/URL - becomes, innate, crepitations, 20mg cialis dosage order cialis online itmeorder come si prende cialis cialis 5 mg efectos secundarios cialis vs warfarin cialis 20 mg ehealthforum cialis trabeculectomy last themselves, http://ekautorepair.com/20mg-cialis-dosage/ buycialise buy cialis online cialis pills in canada http://historicgrandhotels.com/bijwerking-cialis/ bijwerking cialis http://tofupost.com/brandixel-buy-cialis/ cialis effetti ritardanti http://tacticaltomahawkreviews.com/cialis-tadalafil-10mg/ lowest price generic cialis http://gerioliveira.com/mens-health-cialis-alternative/ cialis vs warfarin http://calendr.net/cialis-20-mg/ cialis presentation ppt cow overcome spermatogenesis.

    回复