如果请求超过1450 B,请在PHP中正确读取POST请求参数? [英] Read POST request parameters properly in PHP for request more than 1450 B?
问题描述
我在api.php脚本上收到POST请求。红色内容使用此代码:
I receive POST request on my api.php script. The content red using this code:
$fp = fopen('php://stdin', 'r');
$all = "";
while($line = fgets($fp, 4096)) {
$all .= $line;
}
echo $all;
如下所示:
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="intro"
O
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="title"
T
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="apiKey"
98d32fdsa
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="method"
/media/add
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="upload_field"; filename="original_filename.png"
Content-Type: image/png
------------V2ymHFg03ehbqgZCaKO6jy--
正如你所看到的,我得到4个名为intro,title,apiKey和method的参数,以及一个名为original_filename.png的文件1460字节。
As you can see I get 4 parameters called intro, title, apiKey and method, and one file called original_filename.png with size of 1460 bytes.
不幸的是,我的$ _POST数组是空的。当orginal_filename.png小于1450字节时有什么奇怪$ _POST包含所有参数,而stdin是空的。
Unfortunately, my $_POST array is empty. What is weird when orginal_filename.png is less than 1450 bytes $_POST contains all parameters, and stdin is empty.
我希望能够像正常一样阅读这些字段人类PHP来自$ _POST。
I would like to be able to read these fields like normal human PHP being from $_POST.
如何从$ _POST正确读取此POST请求参数?
What to do in order to read this POST request parameters properly from $_POST?
推荐答案
正如我刚刚在手册中看到的那样,enctype =multipart / form-data不提供原始发布数据,但您也可以尝试使用
As I just read in the manual, raw post data is not available with enctype="multipart/form-data" but can you can also try with
$fp = fopen('php://stdin', 'rb');
请注意 b 标志。
此外,请确保php.ini中 always_populate_post_data = On
Also, make sure that always_populate_post_data=On in your php.ini
这篇关于如果请求超过1450 B,请在PHP中正确读取POST请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!