PHP默认不解析PUT请求体,$_PUT不存在、$_POST为空是原生行为;需通过file_get_contents('php://input')读取原始数据,再依Content-Type(如JSON或表单编码)安全解析,并注意服务器配置与WAF放行。

PHP 默认不自动解析 PUT 请求体,$_PUT 不存在,$_POST 也为空——这不是你配置错了,是 PHP 原生行为。
为什么 $_POST 和 $_GET 都拿不到 PUT 数据
PHP 只对 application/x-www-form-urlencoded 和 multipart/form-data 类型的请求自动解析并填充 $_POST;PUT 请求通常带的是 application/json、application/xml 或纯文本,PHP 不会主动解析,只把原始数据放在 php://input 流里。
- 用
file_get_contents('php://input')才能读到原始 body -
$_SERVER['REQUEST_METHOD']必须是'PUT'才走这条路 - Apache 下若用
.htaccess重写,可能把 PUT 转成 GET/POST,需检查Limit和ScriptMethod配置
如何安全读取并解析 PUT 的 JSON 数据
不能直接用 json_decode($_POST)(它为空),必须从 php://input 读,且要防重复读取——该流只能读一次。
- 务必先判断
$_SERVER['REQUEST_METHOD'] === 'PUT' - 用
$raw = file_get_contents('php://input')获取原始数据 - 检查
$raw是否为空,避免json_decode('')返回null - 用
json_decode($raw, true)转为关联数组,失败时检查json_last_error()
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'PUT') {
$raw = file_get_contents('php://input');
if ($raw === false || $raw === '') {
http_response_code(400);
echo json_encode(['error' => 'Empty PUT body']);
exit;
}
$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON']);
exit;
}
// 此时 $data 是解析后的数组
}
处理 application/x-www-form-urlencoded 类型的 PUT 请求
有些前端(如旧版 jQuery)发 PUT 时仍用表单编码格式,但 PHP 不会自动解析。这时不能依赖 parse_str() 直接处理 php://input,因为键值可能含特殊字符或数组语法。
立即学习“PHP免费学习笔记(深入)”;
- 先读
php://input,再用parse_str($raw, $output)解析 - 注意:PHP 8.1+ 对
parse_str()的安全性更严格,避免传入用户可控的未过滤字符串 - 若后端明确只接受 JSON,建议前端统一设
Content-Type: application/json,避免歧义
Apache / Nginx 下常见拦截问题
即使代码写对了,服务器层也可能拒绝或改写 PUT 请求。
- Apache:确认
mod_rewrite没把 PUT 改成 POST;检查是否允许,或是否被LimitExcept拦截 - Nginx:默认支持 PUT,但若配了
limit_except,需显式加入PUT,例如:limit_except GET HEAD PUT { deny all; } - Cloudflare 或 WAF:部分免费层默认屏蔽非 GET/POST 方法,需在规则中放行 PUT
最常被忽略的是:没检查 Content-Type 就直接解析,结果对 JSON 用了 parse_str,或对表单数据用了 json_decode——类型判断这一步不能跳。











