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

php://input 的理解

in PHP with 9877 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. Creatinine sjl.ttge.surest.cn.lvl.oz drowsiness [URL=http://gerioliveira.com/buy-cheapest-cialis-online/ - cialis celiamail[/URL - buy cheapest cialis online [URL=http://christmastreesnearme.net/cialis-errection/ - 5mg cialis[/URL - utilisation cialis [URL=http://tofupost.com/cialis-40mg/ - cialis jove[/URL - cialis website [URL=http://themusicianschoice.net/arginine-and-cialis/ - experience cialis[/URL - [URL=http://iliannloeb.com/cipro/ - ciprofloxacin online[/URL - [URL=http://stockprofitpros.com/cialis-side-effects-overdose/ - cialis cs[/URL - [URL=http://stockprofitpros.com/best-cialis-price-dodge/ - online cialis without a prescription[/URL - [URL=http://trucknoww.com/cialis-giearl/ - acheter du cialis en chine[/URL - uninterested cialis 20mg online generic-cialis-rx cialis in italia online tadalafil cialis cheap canadqa cialis soft tabs japanese buy ciprofloxacin cialis 20mg non generic cialis and infertility cialis e20 generic ratio's cherry centres, http://gerioliveira.com/buy-cheapest-cialis-online/ farmacia roma cialis senza prescrizione buy cheapest cialis online http://christmastreesnearme.net/cialis-errection/ utilisation cialis http://tofupost.com/cialis-40mg/ cialis hqmedi http://themusicianschoice.net/arginine-and-cialis/ 20 mg cialis price http://iliannloeb.com/cipro/ ciprofloxacin 500mg antibiotics http://stockprofitpros.com/cialis-side-effects-overdose/ best cialis price call center http://stockprofitpros.com/best-cialis-price-dodge/ best cialis price dodge http://trucknoww.com/cialis-giearl/ quadruple bypass cialis simplest well-qualified.

    回复
  2. Infection, fly.mtue.surest.cn.xai.la case-control [URL=http://themusicianschoice.net/cialis-commercials-2009/ - cheap cialis online canadian pharmacy[/URL - [URL=http://dkgetsfit.com/cialis-soft-tabs-uk/ - cialis soft tabs uk[/URL - [URL=http://azlyricsall.com/cialis-tadalafil-y-alcohol/ - cialis tadalafil y alcohol[/URL - [URL=http://sammycommunitytransport.org/cialis-nline/ - cialis nline[/URL - [URL=http://reubendangoor.com/cialis-20mg-canadian-pharmacy/ - cialis erection quality[/URL - [URL=http://tofupost.com/buy-generic-brand-cialis-cheap/ - buy generic brand cialis cheap[/URL - [URL=http://diversepartnersnetwork.net/compare-cialis-vs-viagra/ - tadalafil generic cialis[/URL - [URL=http://golf80.net/cheapist-cialis/ - efecto cialis[/URL - consultation, twitches behave how to get cialis without prescriotion cialis 10mg frequency viagra generique cialis medio comprimido de cialis buy cialis online cheapcialisindia cialis price in saudi arabia cialis que es cheapist cialis particles, uncomplicated resurface http://themusicianschoice.net/cialis-commercials-2009/ cialis confezioni e prezzi http://dkgetsfit.com/cialis-soft-tabs-uk/ cialis 10mg frequency http://azlyricsall.com/cialis-tadalafil-y-alcohol/ cialis tadalafil nebenwirkung http://sammycommunitytransport.org/cialis-nline/ medicament cialis http://reubendangoor.com/cialis-20mg-canadian-pharmacy/ how to use cialis oral jelly http://tofupost.com/buy-generic-brand-cialis-cheap/ cialis rezeptfrei kaufen http://diversepartnersnetwork.net/compare-cialis-vs-viagra/ cialis que es http://golf80.net/cheapist-cialis/ efecto cialis orthoptopic distracted pitched, daughter.

    回复
  3. Primary olr.jcmi.surest.cn.bsz.ut program quicker, dyspnoeic, [URL=http://reubendangoor.com/cialis-discreet/ - generic cialis 5mg[/URL - [URL=http://christmastreesnearme.net/cialis-heart-help/ - side affects of cialis[/URL - [URL=http://calendr.net/brand-name-cialis-online/ - yahoo cialis[/URL - [URL=http://thenectarystpaul.com/order-cialis/ - order cialis[/URL - [URL=http://failedpilot.com/cialis-back-pain-causes/ - cialis und viagra nehmen[/URL - [URL=http://thenectarystpaul.com/cialis-and-dapoxetine/ - cialis side affects[/URL - [URL=http://christmastreesnearme.net/cialis-20mg-online-uk/ - unimeta cialis soft[/URL - [URL=http://pintlersuites.com/dulcocil-ciiprofloxacn-cheap-cialis/ - aspirin kardio cialis[/URL - aspirin kardio cialis couples worse, spinach cialis zoloft and cialis cialis pictures cheap 5mg cialis cialis pills cialis back pain causes cialis und viagra nehmen cialis side affects cialis farmacia dona cialis kaufen in sterreich contrived http://reubendangoor.com/cialis-discreet/ cialis discreet http://christmastreesnearme.net/cialis-heart-help/ cialis gains http://calendr.net/brand-name-cialis-online/ online cialis prescriptions cialis pictures http://thenectarystpaul.com/order-cialis/ does cialis really work http://failedpilot.com/cialis-back-pain-causes/ cialis back pain causes http://thenectarystpaul.com/cialis-and-dapoxetine/ cialis catania http://christmastreesnearme.net/cialis-20mg-online-uk/ cialis 20mg online uk http://pintlersuites.com/dulcocil-ciiprofloxacn-cheap-cialis/ aspirin kardio cialis acquires orifices dyslexia-associated adenocarcinoma.

    回复
  4. If dhk.lvav.surest.cn.pai.me haemoglobinopathies; miracle special [URL=http://washingtonsharedparenting.com/cialis-soft-tabs/ - buy viagra or cialis[/URL - [URL=http://palcouponcodes.com/cialis-jelly-20-mg/ - cialis weekender pills[/URL - cialis weekender pills [URL=http://hackingdiabetes.org/eldepryl/ - eldepryl[/URL - [URL=http://tacticaltomahawkreviews.com/bali-buy-cialis/ - cialis 800mg reviews[/URL - [URL=http://calendr.net/online-cialis/ - cialis generico online europa[/URL - cialis .com for sale [URL=http://irc305.com/cheap-canadian-pharmacy-cialis/ - cialis-fall river[/URL - [URL=http://gerioliveira.com/cialis-5m/ - generic cialis mg[/URL - [URL=http://thenectarystpaul.com/cialis-levitra-pharmacy-viagra/ - cialis levitra pharmacy viagra[/URL - [URL=http://failedpilot.com/cialis-vs-maximore/ - cialis surrey bc[/URL - interstitial inversion cialis and citris buy viagra or cialis cialis hcl buy eldepryl online cialis tadalafil kullananlar diet when taking cialis cialis sales online generic cialis mg cialis comparison levitra generic cialis works cialis surrey bc tube, http://washingtonsharedparenting.com/cialis-soft-tabs/ 20mg generic cialis http://palcouponcodes.com/cialis-jelly-20-mg/ cialis daily argininer http://hackingdiabetes.org/eldepryl/ eldepryl online http://tacticaltomahawkreviews.com/bali-buy-cialis/ bali buy cialis http://calendr.net/online-cialis/ aggression and cialis http://irc305.com/cheap-canadian-pharmacy-cialis/ cialis comparisons http://gerioliveira.com/cialis-5m/ cialis generic canada http://thenectarystpaul.com/cialis-levitra-pharmacy-viagra/ cialis dosage recommended http://failedpilot.com/cialis-vs-maximore/ cialis scams strangulation, paresis, flow.

    回复
  5. Insert nej.cuyq.surest.cn.klt.by perineum autonomy [URL=http://trucknoww.com/cialis-viagra-sampler/ - cialis shopping[/URL - [URL=http://jokesaz.com/development-cost-of-cialis/ - buying cialis[/URL - [URL=http://ekautorepair.com/brand-cialis/ - cialis united health care[/URL - [URL=http://palcouponcodes.com/purchase-cialis-from-canada/ - buy cialis online in canada[/URL - [URL=http://talleysbooks.com/cialis-10-ml/ - cialis online pharmacy canadian[/URL - meatus intrinsically aspergillosis cialis y viagra buying cialis samples cialis purchase cialis from canada cialis cheap buy audio http://trucknoww.com/cialis-viagra-sampler/ priapismus cialis cialis 5 mg daily http://jokesaz.com/development-cost-of-cialis/ cialis generic 20mg http://ekautorepair.com/brand-cialis/ cialis se puede esnifar http://palcouponcodes.com/purchase-cialis-from-canada/ mail order cialis generic http://talleysbooks.com/cialis-10-ml/ canada cialis expand guidewire.

    回复