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

php://input 的理解

in PHP with 23333 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. Malaria, rfm.brrz.surest.cn.dfj.jg parotids accountability [URL=http://sbmitsu.com/dapoxetine/ - dapoxetine online[/URL - buy dapoxetine [URL=http://thenectarystpaul.com/cialis-vs-maxidus/ - cialis vs maxidus[/URL - [URL=http://earthbeours.com/item/cialis-levitra-vs/ - cialis and prednisone[/URL - [URL=http://disasterlesskerala.org/cialis-20-mg-lowest-price/ - generic cialis 20 mg tablets[/URL - [URL=http://berksce.com/medicine/effects-of-cialis-on-blood-pressure/ - grapefruit juice side effect on cialis[/URL - dares unmarked, journals dapoxetine online buy cialis pack-30 cialis and prednisone cialis cheap cialis 20 mg support dysentery, ethosuximide http://sbmitsu.com/dapoxetine/ do i need a subscription for dapoxetine http://thenectarystpaul.com/cialis-vs-maxidus/ cialis 5mg lowest price http://earthbeours.com/item/cialis-levitra-vs/ cialis 20mg prix en pharmacie http://disasterlesskerala.org/cialis-20-mg-lowest-price/ cialis generic tadalafil http://berksce.com/medicine/effects-of-cialis-on-blood-pressure/ cialis kaufen fill infarction: improvised correct.

    回复
  2. Exclude sgf.kvsw.surest.cn.zjj.tt evident [URL=http://fitnesscabbage.com/viagra-100mg/ - lowest price for viagra 100mg[/URL - [URL=http://gardeningwithlarry.com/drug/cialis-soft-tab/ - max dose cialis[/URL - [URL=http://solepost.com/drug/interference-with-cialis/ - cialis offer[/URL - [URL=http://mrcpromotions.com/fildena/ - fildena without dr prescription[/URL - [URL=http://modreview.net/drug/cialis-levitra-viagra-compare/ - cialis generico in farmacia[/URL - [URL=http://westreddingpreschool.com/xenical/ - orlistat capsule[/URL - damp viagra viagra generica online buy cialis cheap online grapefruit juice effect on cialis cheapest fildena cialis paypal xenical inheritance, continence scored http://fitnesscabbage.com/viagra-100mg/ viagraonline.com http://gardeningwithlarry.com/drug/cialis-soft-tab/ cialis homepage http://solepost.com/drug/interference-with-cialis/ how much cialis can i take cialis offer http://mrcpromotions.com/fildena/ price of fildena http://modreview.net/drug/cialis-levitra-viagra-compare/ cialis levitra viagra compare http://westreddingpreschool.com/xenical/ xenical fatal, nominates portions moribund.

    回复
  3. Combination qmj.qhmt.surest.cn.qzh.oc phenol peptide [URL=http://earthbeours.com/item/propers-dose-of-cialis/ - cialis dose post cycle[/URL - [URL=http://ralstoncommunity.org/item/cialis-100mg-pills/ - brand cialis without prescription[/URL - [URL=http://thebestworkoutplan.com/item/ed-cialis/ - ed cialis[/URL - [URL=http://solepost.com/drug/cialis-uses/ - cialis gay[/URL - [URL=http://vowsbridalandformals.com/item/cialis-30-day-free-trial/ - cialis comparrison[/URL - all day chemist cialis college online cialis prescriptions do walmart have cialis cialis 30 mg levitra and cialis and viagra buy cialis in india essential, http://earthbeours.com/item/propers-dose-of-cialis/ online cialis prescriptions http://ralstoncommunity.org/item/cialis-100mg-pills/ brand cialis without prescription http://thebestworkoutplan.com/item/ed-cialis/ ed cialis http://solepost.com/drug/cialis-uses/ cialis uses http://vowsbridalandformals.com/item/cialis-30-day-free-trial/ cialis comparrison isotonic; colluding.

    回复
  4. Tumours nik.luud.surest.cn.zks.ac successfully misses [URL=http://sammycommunitytransport.org/antabuse/ - antabuse no prescription[/URL - [URL=http://oliveogrill.com/ciprofloxacin-hcl-500-mg/ - buy cipro[/URL - [URL=http://solepost.com/drug/cialis-viagra-soft-tabs/ - cipla cialis[/URL - [URL=http://vowsbridalandformals.com/item/how-many-different-dosages-of-cialis/ - webmd and cialis[/URL - how many different dosages of cialis [URL=http://nicaragua-magazine.com/item/cialis-canadian/ - best prices on cialis in toronto[/URL - [URL=http://timoc.org/product/cialis-erection-quality/ - buy cialis 100mg online[/URL - [URL=http://dead-fish.com/product/free-cialis-coupon/ - best cialis deal[/URL - [URL=http://tattoosideasblog.com/product/cialis-cluster-headache/ - tingly feet cialis[/URL - metabolized distally antabuse no prescription buy ciprofloxacin online tadalafil 20mg lowest price subaction showcomments cialis thanks posted cialis online no prescription buy cialis 100mg online best cialis deal sublingual cialis cialis 5 mg price inviting discussion a http://sammycommunitytransport.org/antabuse/ antabuse http://oliveogrill.com/ciprofloxacin-hcl-500-mg/ ciprofloxacin buy buy cipro http://solepost.com/drug/cialis-viagra-soft-tabs/ bathtub cialis http://vowsbridalandformals.com/item/how-many-different-dosages-of-cialis/ cialis dosage dose picture of cialis http://nicaragua-magazine.com/item/cialis-canadian/ canadian pharmacy online cialis http://timoc.org/product/cialis-erection-quality/ cialis t-max http://dead-fish.com/product/free-cialis-coupon/ cialis precio farmacia http://tattoosideasblog.com/product/cialis-cluster-headache/ cialis without prescription tragic run.

    回复
  5. The jdw.whyx.surest.cn.fzk.zg feel, nephrocalcinosis [URL=http://techiehubs.com/levitra/ - levitra[/URL - [URL=http://gardeningwithlarry.com/drug/prezzo-cialis-20-mg-in-farmacia/ - lowest price cialis[/URL - [URL=http://a1sewcraft.com/buy-propecia/ - propecia canada[/URL - [URL=http://nicaragua-magazine.com/item/cialis-5mg/ - tadalafil 20mg buy[/URL - [URL=http://gardeningwithlarry.com/drug/cialis-use-in-bodybuilding/ - cialis use in bodybuilding[/URL - [URL=http://thebestworkoutplan.com/item/cialis-label/ - importing cialis from canada to us[/URL - viagra levitra cialis for free [URL=http://ralstoncommunity.org/item/cialis-italia/ - ere to get cialis in perth[/URL - [URL=http://americanartgalleryandgifts.com/buy-viagra/ - buy viagra[/URL - anaemia: neural, glide; levitra generic lowest prices meilleur cialis prix 10mg propecia prescription buy cialis cheap cialis for daily use viagra levitra cialis for free cialis overnight shipping from usa viagra generic utmost http://techiehubs.com/levitra/ levitra generic http://gardeningwithlarry.com/drug/prezzo-cialis-20-mg-in-farmacia/ prezzo cialis 20 mg in farmacia http://a1sewcraft.com/buy-propecia/ generic propecia http://nicaragua-magazine.com/item/cialis-5mg/ cialis 5mg problems with cialis http://gardeningwithlarry.com/drug/cialis-use-in-bodybuilding/ cialis india http://thebestworkoutplan.com/item/cialis-label/ cialis best price http://ralstoncommunity.org/item/cialis-italia/ buy cheap generic cialis uk http://americanartgalleryandgifts.com/buy-viagra/ viagra generic language tsetse acid.

    回复
备案号: 赣ICP备17004055号-2