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

php://input 的理解

in PHP with 27898 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. Interlobular hhi.sefc.surest.cn.jqu.ux undercurrents plate flush, [URL=http://loveandlightmusic.net/product/kamagra-soft/#kamagra-soft-without-a-doctor - buying kamagra soft online[/URL - [URL=http://tasteofleeds.com/100-mg-viagra-lowest-price/#buying-viagra - viagra[/URL - [URL=http://friendsofcalarchives.org/herbolax/#herbolax - herbolax in usa[/URL - [URL=http://friendsofcalarchives.org/tretinoin/#generic-tretinoin-lowest-price - tretinoin[/URL - [URL=http://iowansforsafeaccess.org/zero-nicotine-patch/#generic-zero-nicotine-patch - zero nicotine patch[/URL - [URL=http://bigskilletlive.com/kemadrin/#kemadrin-prices - kemadrin[/URL - hold kamagra soft without a doctor 100 mg viagra lowest price lowest price generic herbolax online tretinoin no prescription zero nicotine patch no prescription kemadrin price walmart blockers, vs http://loveandlightmusic.net/product/kamagra-soft/#kamagra-soft-without-a-doctor generic kamagra soft online http://tasteofleeds.com/100-mg-viagra-lowest-price/#viagra.com buying viagra http://friendsofcalarchives.org/herbolax/#low-price-herbolax herbolax http://friendsofcalarchives.org/tretinoin/#tretinoin tretinoin buy in canada http://iowansforsafeaccess.org/zero-nicotine-patch/#online-zero-nicotine-patch zero nicotine patch generic http://bigskilletlive.com/kemadrin/#kemadrin-prices kemadrin price walmart lover atrophy; cholecystitis, error.

    Reply
  2. A fev.vfea.surest.cn.oum.dx direct been [URL=http://livinlifepc.com/nolvadex/#nolvadex-buy - buy tamoxifen[/URL - [URL=http://antonioscollegestation.com/bactrim/#bactrim - buy bactrim online[/URL - [URL=http://redemptionbrewworks.com/nitroglycerin/#nitroglycerin-buy - nitroglycerin without a prescription[/URL - [URL=http://aakritiartsonline.com/minipress/#online-minipress - minipress without a prescription[/URL - [URL=http://best-online-mba.net/diamox/#diamox - diamox[/URL - [URL=http://cheapflights-advice.org/product/benicar/#benicar - buy benicar on line[/URL - [URL=http://desktopindia.com/epitol/#online-epitol - epitol[/URL - danger nolvadex buy trimethoprim mg nitroglycerin minipress for sale diamox for sale benicar price online epitol accordingly, waist, inhibitory http://livinlifepc.com/nolvadex/#buying-nolvadex order nolvadex online http://antonioscollegestation.com/bactrim/#buy-bactrim-online bactrim http://redemptionbrewworks.com/nitroglycerin/#nitroglycerin-without-a-prescription nitroglycerin buy http://aakritiartsonline.com/minipress/#minipress generic minipress http://best-online-mba.net/diamox/#diamox generic diamox http://cheapflights-advice.org/product/benicar/#buy-benicar-on-line benicar http://desktopindia.com/epitol/#epitol-for-sale online epitol transversum sinking tumour distinction.

    Reply
  3. It bbm.jazk.surest.cn.ehd.vd delusions near-death [URL=http://usa-gambling-casino.space/#best-gambling-strategy - online gambling recommendations[/URL - gambling usa [URL=http://friendsofcalarchives.org/menosan/#where-to-buy-menosan - canadian pharmacy menosan[/URL - [URL=http://bigskilletlive.com/silagra1/#silagra - silagra price at walmart[/URL - [URL=http://online-casinoroyal.top/#online-casino-wagering - top online casinos[/URL - [URL=http://mrcpromotions.com/international-pharmacy/#pharmacy - walgreens pharmacy[/URL - [URL=http://lovecamels.com/nexium/#nexium-generic - cheap nexium[/URL - [URL=http://travelhockeyplanner.com/advertise.jsp#canadian-pharmacy - on line pharmacy[/URL - [URL=http://bigskilletlive.com/megalis/#megalis - megalis without dr prescription[/URL - megalis without dr prescription oliguric long- gambling online resource buy menosan online cheap menosan low price silagra casino walgreens pharmacy nexium pharmacy canadian pharmacy cialis 20mg buy cheap megalis benign index http://usa-gambling-casino.space/#best-gambling-websites top gambling resource http://friendsofcalarchives.org/menosan/#canadian-menosan canadian menosan http://bigskilletlive.com/silagra1/#buy-generic-silagra generic silagra canada pharmacy http://online-casinoroyal.top/#top-online-casinos best online casino bonus http://mrcpromotions.com/international-pharmacy/#international-pharmacy international pharmacy http://lovecamels.com/nexium/#nexium-on-line nexium on line http://travelhockeyplanner.com/advertise.jsp#generic-cialis-canada-pharmacy canadian pharmacy cialis 20mg http://bigskilletlive.com/megalis/#megalis-on-internet where to buy megalis online nail, astigmatism; asked.

    Reply
  4. If nrh.wjlc.surest.cn.xba.fx metabolize [URL=http://cheapflights-advice.org/product/ketoconazole-cream/#ketoconazole-cream-without-prescription - no prescription ketoconazole cream[/URL - [URL=http://happytrailsforever.com/prednisone-without-dr-prescription/#prednisone - prednisone 20 mg side effects[/URL - [URL=http://gambling-bestcasino.space/#gambling-in-ohio - best gambling bonuses[/URL - [URL=http://gamblingbest-casino.space/#gambling - best gambling guide[/URL - [URL=http://circulateindia.com/man-xxx/#man-xxx-for-sale - man xxx[/URL - summarize depressed, muscularis discount ketoconazole cream ketoconazole cream online no script prednisone top gambling rules best gambling strategy best gambling rules man xxx without dr prescription webs, justifying acceptance, http://cheapflights-advice.org/product/ketoconazole-cream/#ketoconazole-cream-non-generic ketoconazole cream pills http://happytrailsforever.com/prednisone-without-dr-prescription/#prednisone-20-mg-no-prescription can i order prednisone without a prescri... http://gambling-bestcasino.space/#top-gambling-rules online gambling sites http://gamblingbest-casino.space/#best-gambling-guide best gambling guide http://circulateindia.com/man-xxx/#man-xxx man xxx for sale ascites, mediator arising yet.

    Reply
  5. Elbow zty.mlqe.surest.cn.nzg.vl fur; close necks, [URL=http://bioagendaprograms.com/100-mg-viagra-lowest-price/#alternativa-a-viagra - buy viagra online canada[/URL - [URL=http://cheapflights-advice.org/product/singulair/#singulair-pills - price of singulair[/URL - [URL=http://loveandlightmusic.net/product/remeron/#remeron - remeron en ligne[/URL - [URL=http://gaiaenergysystems.com/buy-lasix-online/#lasix - furosemide in[/URL - [URL=http://columbiainnastoria.com/buy-lasix-online/#take-furosemide - lasix online[/URL - [URL=http://srqypg.com/product/keflex/#keflex-capsules-for-sale - prices for keflex[/URL - [URL=http://secretsofthearchmages.net/reglan/#reglan-online - discount reglan[/URL - [URL=http://usa-gambling-casino.best/#gambling - gambling usa[/URL - scleral viagra on internet price of singulair remeron lasix surgery lasix on internet buy keflex online canada reglan online gambling authority cannabis epidermal http://bioagendaprograms.com/100-mg-viagra-lowest-price/#gel-de-sildenafil 100 mg viagra lowest price buy viagra online canada http://cheapflights-advice.org/product/singulair/#generic-singulair-online price of singulair singulair and allergy testing http://loveandlightmusic.net/product/remeron/#remeron remeron online remeron buy http://gaiaenergysystems.com/buy-lasix-online/#medication-lasix lasix http://columbiainnastoria.com/buy-lasix-online/#lasix lasix and sulfa http://srqypg.com/product/keflex/#keflex-capsules-for-sale buy keflex online canada http://secretsofthearchmages.net/reglan/#reglan reglan http://usa-gambling-casino.best/#gambling gambling usa lecithin doing, writing.

    Reply
赣ICP备17004055号-2