post请求与php://input


背景

最近工作中碰到一个奇葩的问题,接手同事的代码,代码竟然把get和post请求做了合并,然后当成get参数转发给其他服务,然后,把php://input的内容当作真正需要post的内容。

可见,这位同学对$_POST数组的由来不是很清楚啊。。。挖了一个大坑。。。。

客户端同学在请求的时候,其实并不清楚post的body部分还有多种编码方式,默认的竟然是multipart/form-data。。。。又一大坑。。。

分析

php从php://input进行数据读取的时候,还真就不读multipart/form-data方式传上来的内容,为啥?因为form-data是为了上传文件用的编码。。让你读的话,难道你自己去写一个解析么?天真。。。

官方说明:

php://input

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to usephp://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where$HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activatingalways_populate_raw_post_data. php://input is not available with enctype=”multipart/form-data”.

注:从php://input中可以读出x-www-form-urlencoded编码的request body


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注