PHP 获取 POST 数据,请使用 php://input,不要使用 $HTTP_RAW_POST_DATA

本文介绍了PHP中从POST请求中读取原始数据的方法,并对比了file_get_contents('php://input')与$HTTP_RAW_POST_DATA的优缺点。推荐使用file_get_contents('php://input'),因为它不依赖特殊的php.ini配置,且在某些情况下占用更少内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请使用下面这个方式来获取 POST 数据:

$post = file_get_contents('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 use php://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 activating always_populate_raw_post_data. php://input is not available with enctype=”multipart/form-data”.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值