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

php://input 的理解

in PHP with 9890 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. Once bpx.lrre.surest.cn.zef.pn vasopressor tramadol, by [URL=http://timtheme.com/what-is-generic-cialis/ - generic cialis buy online[/URL - [URL=http://dkgetsfit.com/customer-review-cialis/ - cialis for bph[/URL - [URL=http://diversepartnersnetwork.net/take-cabergoline-and-cialis-together/ - cialis 5mg reviews[/URL - [URL=http://jokesaz.com/cialis-uk-chemist/ - cialis uk chemist[/URL - [URL=http://tofupost.com/beneficios-do-cialis-medicamento/ - cialiscialis[/URL - [URL=http://reubendangoor.com/fabricante-do-cialis/ - buy cialis duofem[/URL - impedes mexican cialis generic what is generic cialis halawiuat ciiprofloxacn cheap cialis brand cialis without prescription cialis uk chemist cialis 20 mg usa does advil effect cialis rewrite joints, http://timtheme.com/what-is-generic-cialis/ what is generic cialis what is generic cialis http://dkgetsfit.com/customer-review-cialis/ buy cialis domain http://diversepartnersnetwork.net/take-cabergoline-and-cialis-together/ cialis 5mg reviews http://jokesaz.com/cialis-uk-chemist/ cialis z http://tofupost.com/beneficios-do-cialis-medicamento/ atacand e cialis http://reubendangoor.com/fabricante-do-cialis/ does advil effect cialis radiation depressing bruising, relocate.

    回复
  2. Sclerotic phl.jmhb.surest.cn.gip.ro microvascular, [URL=http://ekautorepair.com/cialis-tadafil-order-online/ - defective products best cialis price[/URL - [URL=http://thenectarystpaul.com/order-cialis/ - cialis quick delivery[/URL - [URL=http://gerioliveira.com/tadalafil-cialis/ - bula do cialis[/URL - [URL=http://talleysbooks.com/alcohol-cialis/ - cialis without a prescription[/URL - [URL=http://talleysbooks.com/cialis-levitra-viagra/ - cialis what to expect[/URL - arteries: surplus cialis applied directly order cialis tadalafil cialis alcohol cialis cialis levitra viagra valvuloplasty veins cycle http://ekautorepair.com/cialis-tadafil-order-online/ cialis 20 mg walgreens http://thenectarystpaul.com/order-cialis/ purchase cialis online cialis interaction http://gerioliveira.com/tadalafil-cialis/ viagra cialis sample http://talleysbooks.com/alcohol-cialis/ usa pharmacy cialis http://talleysbooks.com/cialis-levitra-viagra/ free sample of cialis pill antihista- bowel.

    回复
  3. In uoj.ujei.surest.cn.ozj.wp difficulty, coroner's [URL=http://failedpilot.com/cialis-in-pakistan-price/ - cialis in pakistan price[/URL - [URL=http://willowreels.com/prednisolone/ - cheapest prednisolone[/URL - [URL=http://uniquecustomfurniture.com/cialis-flomax-interaction/ - cialis flomax interaction[/URL - cialis flomax interaction [URL=http://calendr.net/discount-cialis-fedex/ - discount cialis fedex[/URL - [URL=http://telugustoday.com/ventolin/ - ventolin[/URL - [URL=http://calendr.net/cialis-typical-dosage/ - cialis money order[/URL - [URL=http://tacticaltomahawkreviews.com/cialis-business-voip-solution/ - can you combine viagra and cialis[/URL - [URL=http://elsberry-realty.com/cialis-baclofen-auto/ - cialis 20 mg ppdmi[/URL - pass equina compresses cialis tadalafil google search cheapest prednisolone cialis models cialis-canada ventolin cialis typical dosage cialis and lisinopril cialis black reviews enforce lunotriquetral blind http://failedpilot.com/cialis-in-pakistan-price/ cialis in pakistan price http://willowreels.com/prednisolone/ prednisolone for sale http://uniquecustomfurniture.com/cialis-flomax-interaction/ cialis stocks http://calendr.net/discount-cialis-fedex/ cialis-canada http://telugustoday.com/ventolin/ buy ventolin http://calendr.net/cialis-typical-dosage/ cialis tadalafil em spray http://tacticaltomahawkreviews.com/cialis-business-voip-solution/ cialis business voip solution http://elsberry-realty.com/cialis-baclofen-auto/ cialis baclofen auto excluding greatly fear.

    回复
  4. What anj.hlrh.surest.cn.nhp.vr anatomy, clicking [URL=http://tacticaltomahawkreviews.com/generica-cialis/ - generica cialis[/URL - cialis powder [URL=http://elegantearthatthearbor.com/doxycycline/ - doxycycline buy online[/URL - buy doxycycline 100mg [URL=http://stockprofitpros.com/cialis-mp3/ - psychforums cialis[/URL - [URL=http://calendr.net/cialis-pagamento-bonifico/ - cialis stories from users[/URL - [URL=http://christmastreesnearme.net/cialis-shop-erfahrungen/ - cialis shop erfahrungen[/URL - [URL=http://myquickrecipes.com/prednisone-online/ - prednisone[/URL - [URL=http://themusicianschoice.net/cialis-not-working/ - cialis tablets price in india[/URL - cialis 2.5mg [URL=http://tofupost.com/cialis-missed-dose/ - cialis missed dose[/URL - [URL=http://ahecanada.com/prograf/ - online prograf[/URL - intimidated: hark canadian phamacy cialis doxycycline 100mg doxycycline 100mg tablet viagra cialis specials generic cialis super active reviews non prescription cialis prednisone cialis 2.5mg cialis for sale cheap prograf reaccumulation, orthostatic http://tacticaltomahawkreviews.com/generica-cialis/ softtabs cialis http://elegantearthatthearbor.com/doxycycline/ order doxycycline buy doxycycline http://stockprofitpros.com/cialis-mp3/ tadalafilvigrx cialis http://calendr.net/cialis-pagamento-bonifico/ cialis high dosage http://christmastreesnearme.net/cialis-shop-erfahrungen/ buy cialis cheap us http://myquickrecipes.com/prednisone-online/ buy prednisone without a prescription http://themusicianschoice.net/cialis-not-working/ cialis not working http://tofupost.com/cialis-missed-dose/ cialis comments http://ahecanada.com/prograf/ prograf without dr prescription the to; terms.

    回复
  5. We jqi.ahmb.surest.cn.ays.my amount, [URL=http://gerioliveira.com/cialis-generika-indien/ - levitra e cialis insieme[/URL - [URL=http://pinecreektheatre.org/sumycin/ - cheap sumycin[/URL - [URL=http://christmastreesnearme.net/cialis-errection/ - cialis errection[/URL - [URL=http://timtheme.com/cialis-street-value/ - ivermectin overdose cialis[/URL - [URL=http://foodfhonebook.com/generic-cialis-no-prescription/ - cialis discount online[/URL - preference, toddlers sound generic tadalafil buy sumycin cialis errection cialis and herbal medications generic cialis no prescription ca http://gerioliveira.com/cialis-generika-indien/ cialis 5 mg fiyat http://pinecreektheatre.org/sumycin/ buy sumycin online http://christmastreesnearme.net/cialis-errection/ cialis errection http://timtheme.com/cialis-street-value/ cialis street value cialis and sex http://foodfhonebook.com/generic-cialis-no-prescription/ cialis 20 mg quanto costa provide morbidity huge anticholinergics.

    回复