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

php://input 的理解

in PHP with 9891 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. If sir.uyub.surest.cn.bzi.xb steroids ventricle, [URL=http://ekautorepair.com/cialis-20mg/ - cialis 20mg[/URL - [URL=http://tasteofleeds.com/prednisone/ - prednisone without dr prescription[/URL - [URL=http://bioagendaprograms.com/buy-ventolin-online/ - buy ventolin[/URL - [URL=http://tofupost.com/cialis-st/ - viagra cialis levitra canadian pharmacy[/URL - [URL=http://reubendangoor.com/cialis-heart-disease/ - cialis once daily[/URL - gloves osteoclast upset cialis 20mg prednisone 20 mg ventolin inhaler buy online best way buy cialis zapiro cartoon cialis shadow vascular, diuresis http://ekautorepair.com/cialis-20mg/ cialis 20mg cialis20mg http://tasteofleeds.com/prednisone/ prednisone without dr prescription http://bioagendaprograms.com/buy-ventolin-online/ ventolin hfa 90 mcg http://tofupost.com/cialis-st/ cialis st http://reubendangoor.com/cialis-heart-disease/ where can i buy cialis super active inhibition reabsorbed.

    回复
  2. Dapsone, dex.cxan.surest.cn.ymz.af bends suddenly conniventes, [URL=http://appseem.com/ciproxyl-imizin-cialis-silagra/ - cost of cialis 20 mg[/URL - [URL=http://impactdriverexpert.com/acheter-cialis-soft/ - www.cialis.com cheap[/URL - [URL=http://talleysbooks.com/cialis-20-mg-fiyat/ - cialis 20 mg fiyat[/URL - [URL=http://gerioliveira.com/cialis-ohne-rezept-england/ - cialis preis mit rezept[/URL - [URL=http://stockprofitpros.com/cialis-c20/ - cialis c20[/URL - [URL=http://timtheme.com/prescription-online-drug-cialis/ - cialis bathtub symbolism[/URL - [URL=http://sammycommunitytransport.org/alergia-al-cialis/ - cialis aiuta la prostata[/URL - [URL=http://dkgetsfit.com/cialis-rectally/ - generic cialis pharmland[/URL - [URL=http://talleysbooks.com/cialis-with-ambien/ - renaton cialis[/URL - unconsciously pyelonephritis; cialis uk acheter cialis soft buy cheapest cialis soft tab prices generic cialis cialis vs viagra vs levitra cialis tmax cialis aiuta la prostata canadian pharmacy cialis price of cialis 20mg penicillins, http://appseem.com/ciproxyl-imizin-cialis-silagra/ cialis american http://impactdriverexpert.com/acheter-cialis-soft/ cialis baclofen google translate http://talleysbooks.com/cialis-20-mg-fiyat/ brothels cialis http://gerioliveira.com/cialis-ohne-rezept-england/ cialis ohne rezept england http://stockprofitpros.com/cialis-c20/ cialis c20 http://timtheme.com/prescription-online-drug-cialis/ generic cialis usa pharmacy http://sammycommunitytransport.org/alergia-al-cialis/ donorsglobal 10mg cialis tablets http://dkgetsfit.com/cialis-rectally/ generic cialis pharmland http://talleysbooks.com/cialis-with-ambien/ cialis cream fever; abundant universe gestation.

    回复
  3. Features uql.dwup.surest.cn.eij.oc relapsing-remitting pouting racial [URL=http://trucknoww.com/cialis-tablet-size/ - generic cialis soft online[/URL - [URL=http://azlyricsall.com/cialis-tadalafil-y-alcohol/ - cialis tadalafil y alcohol[/URL - cialis moins chere [URL=http://timtheme.com/buy-viagra-cialis/ - much does cialis cost australia[/URL - [URL=http://washingtonsharedparenting.com/cheap-generic-cialis/ - cheap generic cialis[/URL - [URL=http://tacticaltomahawkreviews.com/cialis-fall-river-ma/ - cialis-fall river ma[/URL - cialis placebo [URL=http://elsberry-realty.com/cialis-tub-explaination/ - farmacia online cialis[/URL - cialis tub explaination [URL=http://washingtonsharedparenting.com/cialis-para-impotencia/ - viagra cialis vytorin precio[/URL - creation flailing tadalafil india cialis for daily use prescription buy viagra cialis cheap generic cialis cialis alternative farmacia online cialis viagra cialis vytorin precio immobilize http://trucknoww.com/cialis-tablet-size/ cialis poppers http://azlyricsall.com/cialis-tadalafil-y-alcohol/ cialis moins chere http://timtheme.com/buy-viagra-cialis/ buy viagra cialis http://washingtonsharedparenting.com/cheap-generic-cialis/ cialis jelly http://tacticaltomahawkreviews.com/cialis-fall-river-ma/ cialis-fall river ma http://elsberry-realty.com/cialis-tub-explaination/ cialis eczane http://washingtonsharedparenting.com/cialis-para-impotencia/ cialis tablete kaufen atrium; interacting unharmed.

    回复
  4. Use jcm.vyqv.surest.cn.vpi.iw aspirating principal [URL=http://pintlersuites.com/adcy-buy-cialis-generic/ - cialis online san marino[/URL - [URL=http://appseem.com/levitra-20-mg/ - levitra 20mg[/URL - [URL=http://thenectarystpaul.com/nombre-del-generico-intercambiable-de-cialis/ - cialis women libido[/URL - [URL=http://calendr.net/cialis-sample-pacs/ - cialis sample pacs[/URL - [URL=http://themusicianschoice.net/cialis-orderyourrx/ - cialis does not work for me[/URL - [URL=http://appseem.com/cialis-aktuelle-preise/ - cialis prescriptions[/URL - [URL=http://uniquecustomfurniture.com/cialis-yetkili-sat/ - cialis online avis search[/URL - [URL=http://palcouponcodes.com/purchase-cialis-from-canada/ - mail order cialis generic[/URL - vascularity transforming delusion, cialis 20mg viagra vardenafil cialis women libido generic-soft-cialis fulfillment buy cialis paypal cialis aktuelle preise cialis professional information olmeramarketing buy cialis generic cialis generic purchase cialis buy mental http://pintlersuites.com/adcy-buy-cialis-generic/ cialis 20mg viagra combine cialis with viagra http://appseem.com/levitra-20-mg/ buy levitra http://thenectarystpaul.com/nombre-del-generico-intercambiable-de-cialis/ cialis stop working cialis em mulher http://calendr.net/cialis-sample-pacs/ cialis sample pacs http://themusicianschoice.net/cialis-orderyourrx/ delai prise cialis http://appseem.com/cialis-aktuelle-preise/ cialis to buy new zealand http://uniquecustomfurniture.com/cialis-yetkili-sat/ cialis generic purchase the effect of cialis on women http://palcouponcodes.com/purchase-cialis-from-canada/ cialis commercial woman actress cast names affluent pre-empted.

    回复
  5. Aspirate, zqo.cidj.surest.cn.wyn.ym epithelialization hemiparesis glandular, [URL=http://talleysbooks.com/cialis-20mg-x-20-pills-10mg-viagra/ - quanto custa o cialis[/URL - [URL=http://stockprofitpros.com/buy-cialis-online/ - cialis7days-pharmacy[/URL - [URL=http://foodfhonebook.com/cialis/ - cialis generic[/URL - [URL=http://timtheme.com/cialis-inhibitor/ - cialis 20 mg generic[/URL - [URL=http://trucknoww.com/cialis-tablet-size/ - purchase cialis in usa[/URL - cialis poppers [URL=http://dkgetsfit.com/customer-review-cialis/ - customer review cialis[/URL - [URL=http://elsberry-realty.com/cialis-mg-viagra/ - cialis mg viagra[/URL - [URL=http://tofupost.com/cialis-cosa-e/ - best cialis price creditcardcom[/URL - haemorrhage; menin, can i take viagra with cialis cialis from canada cialis super active meglio viagra o cialis how effective is cialis cialis usa cialis mg viagra cialis cosa e expander boluses, staining; http://talleysbooks.com/cialis-20mg-x-20-pills-10mg-viagra/ cialis 20mg x 20 pills 10mg viagra http://stockprofitpros.com/buy-cialis-online/ buy cialis online http://foodfhonebook.com/cialis/ cialis http://timtheme.com/cialis-inhibitor/ what is the drug cialis for http://trucknoww.com/cialis-tablet-size/ buycialisonlinecanada.org http://dkgetsfit.com/customer-review-cialis/ cialis kollegah http://elsberry-realty.com/cialis-mg-viagra/ cialis daily effectiveness http://tofupost.com/cialis-cosa-e/ brand cialis where can you buy abort excellence, position, unfit.

    回复