在使用 print_r(file_get_contents(php://input)) 之后,却可以得到json数据
那什么是php://input呢?
对php://input的介绍,php官方手册文档有一段话对它进行了很明确地概述。
“php://input allows you to read raw post data. it is a less memory intensive alternative to $http_raw_post_data and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.
翻译过来,是这样:
“php://input可以读取没有处理过的post数据。相较于$http_raw_post_data而言,它给内存带来的压力较小,并且不需要 特殊的php.ini设置。php://input不能用于enctype=multipart/form-data”。
1,content-type 取值为application/x-www-form-urlencoded时,php会将http请求body相应数据会填入到数组$_post,填入 到$_post数组中的数据是进行urldecode()解析的结果。(其实,除了该content-type,还有multipart/form- data表示数据是表单数据,稍后我们介绍)
2,php://input数据,只要content-type不为multipart/form-data(该条件限制稍后会介绍)。那么php: //input数据与http entity body部分数据是一致的。该部分相一致的数据的长度由content-length指定。
3,仅当content-type为application/x-www-form-urlencoded且提交方法是post方法时,$_post数 据与php://input数据才是”一致”(打上引号,表示它们格式不一致,内容一致)的。其它情况,它们都不一致。
4,php://input读取不到$_get数据。是因为$_get数据作为query_path写在http请求头部(header)的path字段,而不是写在http请求的body部分。
相信大家对 php://input已经有一定深度地了解了。那么$http_raw_post_data是什么呢?$http_raw_post_data是php 内置的一个全局变量。它用于,php在无法识别的content-type的情况下,将post过来的数据原样地填入变 量$http_raw_post_data。它同样无法读取content-type为multipart/form-data的post数据。需要设置 php.ini中的always_populate_raw_post_data值为on,php才会总把post数据填入变 量$http_raw_post_data。
学习笔记1,coentent-type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,php才会将http请求数据包中相应的数据填入全局变量$_post
2,php不能识别的content-type类型的时候,会将http请求包中相应的数据填入变量$http_raw_post_data
3, 只有coentent-type不为multipart/form-data的时候,php不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由coentent-length指定。
4,只有content-type为application/x-www-data-urlencoded时,php://input数据才跟$_post数据相一致。
5,php://input数据总是跟$http_raw_post_data相同,但是php://input比$http_raw_post_data更凑效,且不需要特殊设置php.ini
6,php会将path字段的query_path部分,填入全局变量$_get。通常情况下,get方法提交的http请求,body为空。
