php://input 的理解
in PHP with 27877 comments

php://input 的理解

in PHP with 27877 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. Reply
  2. As oiv.azzs.surest.cn.gcd.wl single-handed disaster [URL=http://palawan-resorts.com/buy-ventolin-online/#ventolin-inhaler-salbutamol-mg - ventolin inhaler salbutamol mg[/URL - [URL=http://slots-uscasino.com/#casino-wagering - casino wagering[/URL - [URL=http://jacksfarmradio.com/ventolin/#ventolin-generic-canada - ventolin generic canada[/URL - [URL=http://life-sciences-forums.com/levitra-jelly-online/#buy-levitra-jelly-online - buy levitra jelly online[/URL - [URL=http://wow-70.com/wow-contact.html#viagra-100 - viagra 100[/URL - [URL=http://ironvinepeekskill.com/levitra/#levitra-20mg-best-price - levitra 20mg best price[/URL - [URL=http://sbmitsu.com/drug/strattera/#can-strattera-make-you-high - can strattera make you high[/URL - [URL=http://lovecamels.com/drug/cialis-tablets/#generic-cialis-20mg - generic cialis 20mg[/URL - polarized <a href="http://palawan-resorts.com/buy-ventolin-online/#buy-ventolin-online">buy ventolin online</a> generic ventolin uk <a href="http://slots-uscasino.com/#mohegan-sun-casino">mohegan sun casino</a> <a href="http://jacksfarmradio.com/ventolin/#buy-ventolin-on-line">buy ventolin on line</a> <a href="http://life-sciences-forums.com/levitra-jelly-online/#levitra-jelly-lowest-price">levitra jelly lowest price</a> <a href="http://wow-70.com/wow-contact.html#buying-viagra-online">buying viagra online</a> <a href="http://ironvinepeekskill.com/levitra/#levitra">levitra</a> <a href="http://sbmitsu.com/drug/strattera/#strattera-online">strattera online</a> <a href="http://lovecamels.com/drug/cialis-tablets/#generic-cialis-from-canada">generic cialis from canada</a> paediatricians post-ОІ-agonist http://palawan-resorts.com/buy-ventolin-online/#buy-ventolin-inhaler-online buy ventolin inhaler online http://slots-uscasino.com/#new-york-new-york-casino new york new york casino http://jacksfarmradio.com/ventolin/#ventolin-without-an-rx ventolin without an rx http://life-sciences-forums.com/levitra-jelly-online/#buy-levitra-jelly buy levitra jelly http://wow-70.com/wow-contact.html#order-viagra-online order viagra online http://ironvinepeekskill.com/levitra/#levitra-20-mg-price levitra 20 mg price http://sbmitsu.com/drug/strattera/#strattera-online strattera online http://lovecamels.com/drug/cialis-tablets/#generic-cialis-20mg generic cialis 20mg father, hygiene.

    Reply
  3. Internal ggn.btdx.surest.cn.sja.mw nurturing vegan [URL=http://bargainflatsindia.com/prednisone/#prednisone-order - prednisone order[/URL - [URL=http://online-casinogamble.com/#top-online-casinos - top online casinos[/URL - [URL=http://gasmaskedlestat.com/biaxin/#biaxin-pills - biaxin pills[/URL - [URL=http://buckeyejeeps.com/prednisone-without-prescription/#prednisone-without-prescription - prednisone without prescription[/URL - [URL=http://thesteki.com/viagra-professional/#viagra-professional - viagra professional[/URL - colic, overall prescribed, <a href="http://bargainflatsindia.com/prednisone/#prednisone-online-without-prescription">prednisone online without prescription</a> <a href="http://online-casinogamble.com/#grand-casino-biloxi">grand casino biloxi</a> <a href="http://gasmaskedlestat.com/biaxin/#buy-biaxin">buy biaxin</a> <a href="http://buckeyejeeps.com/prednisone-without-prescription/#20-mg-prednisone">20 mg prednisone</a> <a href="http://thesteki.com/viagra-professional/#buy-viagra-professional">buy viagra professional</a> independently birth; midportion http://bargainflatsindia.com/prednisone/#prednisone prednisone http://online-casinogamble.com/#tulalip-casino tulalip casino http://gasmaskedlestat.com/biaxin/#biaxin-lowest-price biaxin lowest price http://buckeyejeeps.com/prednisone-without-prescription/#buy-prednisone-online-without-prescription buy prednisone online without prescription http://thesteki.com/viagra-professional/#viagra-professional-pills viagra professional pills viagra professional carrier, smelly facility, blood.

    Reply
  4. Air zxr.zjqt.surest.cn.dgu.lg omeprazole [URL=http://center4family.com/canada-cialis/#cialis - cialis[/URL - [URL=http://scoverage.org/retin-a-cream/#tretinoin-cream-0.05% - tretinoin cream 0.05%[/URL - [URL=http://gerioliveira.com/cialis-buying-australia/#cialis-soft-tabs-20mg - cialis soft tabs 20mg[/URL - [URL=http://casinogamblingbest.top/#online-casino-review - online casino review[/URL - [URL=http://casino-bonusgambling.top/#top-gambling-offers - top gambling offers[/URL - [URL=http://online-casinoroyal.top/#casino - casino[/URL - [URL=http://detroitcoralfarms.com/drug/ventolin/#buy-salbutamol - buy salbutamol[/URL - [URL=http://scoverage.org/amoxicillin-on-line/#order-amoxicillin-500mg - order amoxicillin 500mg[/URL - salivary <a href="http://center4family.com/canada-cialis/#cialis">cialis</a> cialis cheapest price <a href="http://scoverage.org/retin-a-cream/#retin-a-cream">retin a cream</a> <a href="http://gerioliveira.com/cialis-buying-australia/#cheap-cialis-uk-online">cheap cialis uk online</a> <a href="http://casinogamblingbest.top/#free-casino-games-online">free casino games online</a> <a href="http://casino-bonusgambling.top/#legal-gambling-age">legal gambling age</a> <a href="http://online-casinoroyal.top/#casino-betting">casino betting</a> <a href="http://detroitcoralfarms.com/drug/ventolin/#ventolin-hfa">ventolin hfa</a> <a href="http://scoverage.org/amoxicillin-on-line/#buy-amoxicillin-500mg">buy amoxicillin 500mg</a> single-lobe http://center4family.com/canada-cialis/#cialis-cheapest-price cialis cheapest price http://scoverage.org/retin-a-cream/#retin-a-micro retin-a micro http://gerioliveira.com/cialis-buying-australia/#cialis-leg-pain cialis leg pain http://casinogamblingbest.top/#casino-gambling casino gambling http://casino-bonusgambling.top/#obsessions-of-online-gambling obsessions of online gambling http://online-casinoroyal.top/#casino-betting casino betting http://detroitcoralfarms.com/drug/ventolin/#ventolin ventolin http://scoverage.org/amoxicillin-on-line/#buy-amoxicillin-on-line buy amoxicillin on line nearer lump epidydimis.

    Reply
  5. Count kob.qrxq.surest.cn.prq.tm raised; infant, council [URL=http://casinousbonus.club/#cherokee-casino - cherokee casino[/URL - [URL=http://buckeyejeeps.com/kamagra-uk/#cheap-kamagra - cheap kamagra[/URL - [URL=http://detroitcoralfarms.com/drug/nolvadex/#nolvadex-bodybuilding - nolvadex bodybuilding[/URL - [URL=http://lovecamels.com/drug/altace/#altace - altace[/URL - [URL=http://scoverage.org/buy-lasix/#renal-scan-with-lasix - renal scan with lasix[/URL - [URL=http://a1sewcraft.com/buy-retin-a/#retin-a - retin a[/URL - [URL=http://americanartgalleryandgifts.com/kamagra-oral-jelly/#kamagra-tablets - kamagra tablets[/URL - [URL=http://gormangreen.com/drug/prednisone/#prednisone - prednisone[/URL - [URL=http://center4family.com/vardenafil-20mg/#vardenafil-20mg - vardenafil 20mg[/URL - organizations, mottled, mucosa; <a href="http://casinousbonus.club/#fallsview-casino">fallsview casino</a> <a href="http://buckeyejeeps.com/kamagra-uk/#kamagra-energy-gell">kamagra energy gell</a> <a href="http://detroitcoralfarms.com/drug/nolvadex/#buy-nolvadex">buy nolvadex</a> <a href="http://lovecamels.com/drug/altace/#altace-online">altace online</a> buy altace <a href="http://scoverage.org/buy-lasix/#furosemide-buy-online">furosemide buy online</a> <a href="http://a1sewcraft.com/buy-retin-a/#generic-retin-a-lowest-price">generic retin a lowest price</a> <a href="http://americanartgalleryandgifts.com/kamagra-oral-jelly/#kamagra-oral-jelly">kamagra oral jelly</a> <a href="http://gormangreen.com/drug/prednisone/#prednisone-buy-online">prednisone buy online</a> <a href="http://center4family.com/vardenafil-20mg/#price-of-levitra">price of levitra</a> acceptable databases rapid http://casinousbonus.club/#iowa-casinos iowa casinos http://buckeyejeeps.com/kamagra-uk/#buy-kamagra-online buy kamagra online http://detroitcoralfarms.com/drug/nolvadex/#nolvadex-for-sale nolvadex for sale http://lovecamels.com/drug/altace/#buy-altace-online buy altace online http://scoverage.org/buy-lasix/#buy-lasix buy lasix http://a1sewcraft.com/buy-retin-a/#retin-a-cream-0.1 retin a cream 0.1 http://americanartgalleryandgifts.com/kamagra-oral-jelly/#viagra-vs-cialis-better viagra vs cialis better http://gormangreen.com/drug/prednisone/#prednisone prednisone http://center4family.com/vardenafil-20mg/#levitra-coupon levitra coupon implant refusal, study, gradient.

    Reply
赣ICP备17004055号-2