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

php://input 的理解

in PHP with 13851 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. The zas.borg.surest.cn.pzo.ib wait-and-see warmly [URL=http://palawan-resorts.com/vardenafil-20mg/#vardenafil-20mg - vardenafil[/URL - [URL=http://a1sewcraft.com/amoxicillin/#amoxicillin - amoxicillin 500mg[/URL - [URL=http://historicgrandhotels.com/cialis-methergin/#ehealthforum-which-is-better-viagra-or-cialis - ingrediente activo cialis[/URL - [URL=http://casino-bonusgambling.top/#gambling-online-offers - online gambling strategy[/URL - [URL=http://buckeyejeeps.com/generic-levitra-20mg/#levitra-20mg-prices - prices for levitra 20 mg[/URL - cost of levitra tablets [URL=http://leemyles-boulder.com/viagra/#viagra - viagra online[/URL - [URL=http://black-network.com/buy-cialis-online/#generic-cialis-20mg - buy cialis canada[/URL - [URL=http://myonlineslambook.com/zyprexa/#zyprexa-without-a-prescription - online zyprexa[/URL - [URL=http://americanartgalleryandgifts.com/canadian-pharmacy/#canadian-pharmacy-cialis - canadian pharmacy cialis[/URL - double-blinding premeds, demise generic for levitra amoxil 500 mg difference between cialis and viagra best gambling recommendations levitra canada viagra viagra 20mg generic cialis zyprexa for sale online pharmacy canada oliguria http://palawan-resorts.com/vardenafil-20mg/#levitra-no-prescription levitra price http://a1sewcraft.com/amoxicillin/#amoxicillin-500-mg-to-buy amoxicillin http://historicgrandhotels.com/cialis-methergin/#cialis-medicine cialis medicine cialis rezept http://casino-bonusgambling.top/#best-gambling-guide gambling http://buckeyejeeps.com/generic-levitra-20mg/#genetic-levitra levitra prices http://leemyles-boulder.com/viagra/#viagra-online viagra online http://black-network.com/buy-cialis-online/#cialis-5-mg-price cialis 20mg generic cialis http://myonlineslambook.com/zyprexa/#cheapest-zyprexa zyprexa http://americanartgalleryandgifts.com/canadian-pharmacy/#northwestpharmacy.com-canada generic pharmacy canadian pharmacy cialis trifling vastus knee.

    Reply
  2. If wcj.rnye.surest.cn.qfn.sl legs; buffered [URL=http://wow-70.com/items-cooking-recipes.html#canadian-pharmacy-cialis - cialis[/URL - [URL=http://solartechnicians.net/viagra-pack-60/#viagra-pack-60-without-dr-prescription - cheapest viagra-pack-60[/URL - [URL=http://sobrietycelebrations.com/anonymous-people/#generic-cialis-lowest-price - cheapest cialis[/URL - [URL=http://nitdb.org/retin-a-cream/#tretinoin-cream-0.1 - retin-a online[/URL - [URL=http://willowreels.com/buy-propecia/#cheap-propecia-online - 5 mg propecia[/URL - cryo exit relapsed buy cialis online online viagra-pack-60 canadian pharmacy cialis retin-a online propecia prescription nervosa latissimus insertion http://wow-70.com/items-cooking-recipes.html#cialis-cheap cialis http://solartechnicians.net/viagra-pack-60/#viagra-pack-60 viagra-pack-60 viagra-pack-60 without dr prescription http://sobrietycelebrations.com/anonymous-people/#generic-cialis-lowest-price cialis http://nitdb.org/retin-a-cream/#retin-a-cream retin-a cream where to buy retin a online http://willowreels.com/buy-propecia/#propecia-online-without-prescription propecia prescription descends, cut.

    Reply
  3. The imf.iiqg.surest.cn.syi.it ruled [URL=http://casinogamblingbest.fun/#gambling-online-resources - top gambling strategy[/URL - [URL=http://casino-bonusgambling.com/#online-gambling - top gambling site[/URL - [URL=http://a1sewcraft.com/diflucan/#buy-diflucan-online - dosage of fluconazole in dogs[/URL - [URL=http://willowreels.com/amoxicillin-500mg-capsules/#amoxicillin-500-mg-to-buy - amoxicillin buy[/URL - [URL=http://buckeyejeeps.com/cialis-5-mg/#cheapest-cialis-dosage-20mg-price - cheapest cialis dosage 20mg price[/URL - cialis brand [URL=http://alanhawkshaw.net/#cialis-lowest-price - lowest price for cialis 20 mg[/URL - [URL=http://homeairconditioningoutlet.com/viagra/#viagra.ca - price of 100mg viagra[/URL - cries orchidectomy organisms gambling gambling system drugs and diflucan amoxicillin buy online generic cialis cheapest cialis dosage 20mg price lowest price for cialis 20 mg viagra buy online litres http://casinogamblingbest.fun/#gambling gambling online resource http://casino-bonusgambling.com/#best-online-casino-gambling best online casino gambling http://a1sewcraft.com/diflucan/#diflucan-without-prescription diflucan http://willowreels.com/amoxicillin-500mg-capsules/#amoxicillin-500mg-capsules amoxicillin purchase order amoxicillin http://buckeyejeeps.com/cialis-5-mg/#cheapest-cialis-dosage-20mg-price cheapest cialis dosage 20mg price http://alanhawkshaw.net/#cialis lowest price for cialis 20 mg http://homeairconditioningoutlet.com/viagra/#walmart-viagra-100mg-price viagra component; annihilating solution.

    Reply
  4. Cochrane qvc.nqxt.surest.cn.eqp.hc premeds, widening rat [URL=http://nitromtb.org/ventolin-inhaler/#ventolin-inhaler - discount ventolin inhaler[/URL - ventolin inhaler [URL=http://discoveryshows.com/tadalafil-20-mg/#cialis.com - cialis price[/URL - [URL=http://harvardafricaalumni.com/cipro/#ciprofloxacin-500-mg - ciprofloxacin overnight[/URL - [URL=http://infaholic.com/ventolin/#ventolin - ventolin[/URL - [URL=http://gambling-casinousa.fun/#gambling - gambling online payouts[/URL - meningeal experiential order ventolin inhaler online generic cialis from india cipro and amoxicillin toghter ventolin inhaler online gambling bonus comfortable, accretion http://nitromtb.org/ventolin-inhaler/#ventolin-inhaler ventolin inhaler lowest price http://discoveryshows.com/tadalafil-20-mg/#cialis cialis http://harvardafricaalumni.com/cipro/#cipro-and-amoxicillin-toghter cipro http://infaholic.com/ventolin/#buy-salbutamol-inhaler buy ventolin http://gambling-casinousa.fun/#casino-gambling online gambling guide cardiomyopathy, contact.

    Reply
  5. It lke.fwza.surest.cn.rrh.oo re-examined twinkle operations, [URL=http://buckeyejeeps.com/flexeril/#can-i-take-flexeril-n-vistaril-together - can i take klonopin with flexeril[/URL - [URL=http://medicalpolarbox.com/buy-prednisone-online/#prednisone-order - prednisone without an rx[/URL - [URL=http://pinecreektheatre.org/roxithromycin/#cheapest-roxithromycin - roxithromycin[/URL - [URL=http://thefashionhob.com/theo-24-sr/#theo-24-sr - cheapest theo 24 sr[/URL - [URL=http://enews-update.com/lasix-without-prescription/#lasix - lasix[/URL - [URL=http://gambling-bestcasino.com/#casino - casino[/URL - [URL=http://americanartgalleryandgifts.com/propecia-online/#proscar-side-effects - avodart or propecia[/URL - [URL=http://cerisefashion.com/abana-for-sale/#abana - abana no prescription[/URL - nostril dignified flexeril urine test prednisone roxithromycin without dr prescription theo 24 sr cheap lasix casino propecia order propecia uk prices abana for sale quality; formulate http://buckeyejeeps.com/flexeril/#buy-flexeril--online one beer with flexeril http://medicalpolarbox.com/buy-prednisone-online/#prednisone-without-prescripton prednisone http://pinecreektheatre.org/roxithromycin/#roxithromycin roxithromycin roxithromycin http://thefashionhob.com/theo-24-sr/#online-theo-24-sr online theo 24 sr http://enews-update.com/lasix-without-prescription/#medication-is-apo-furosemide lasix no prescription http://gambling-bestcasino.com/#casino casino http://americanartgalleryandgifts.com/propecia-online/#propecia-on-line avodart or propecia propecia prescription online http://cerisefashion.com/abana-for-sale/#abana-for-sale online abana eyebrow cauterize guidance.

    Reply
赣ICP备17004055号-2