我正在测试我的 API,GET 和 POST 方法都很好,但是当我尝试 PUT 或 DELETE 时,它给出了 405 错误消息。我在 docker 上使用 PHP 7.3 Mysql nginx 1.25.3 并使用 postman 进行测试
我已经尝试使用 limit_except 和 dav_methods 明确允许 .conf 文件中的方法,并检查了 http_dav_module 的安装参数
server {
index index.html index.php;
server_name docker.localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access`.log;
root /www;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
allow all;
satisfy any;
limit_except GET POST PUT DELETE {
deny all;
}
dav_methods PUT DELETE;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
下面是我的 php POST 处理程序
case 'PUT':
$data = json_decode(file_get_contents('php://input'), true);
// Check if required fields are present in the request
if (!isset($data['email'], $data['pwrd'], $data['username'], $data['surname'], $data['nickname'])) {
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Incomplete data provided']);
exit;
}
// Extract data from the request
$email = $data['email'];
$password = $data['pwrd'];
$username = $data['username'];
$surname = $data['surname'];
$nickname = $data['nickname'];
// Prepare and execute a database query to update the user by email
$stmt = $pdo->prepare('UPDATE main SET pwrd=?, username=?, surname=?, nickname=? WHERE email=?');
$stmt->execute([$password, $username, $surname, $nickname, $email]);
// Check if any rows were affected
$rowCount = $stmt->rowCount();
if ($rowCount > 0) {
echo json_encode(['message' => 'User updated successfully']);
} else {
http_response_code(404); // Not Found
echo json_encode(['error' => 'User not found']);
}
break;