2026-06-02 20:46:13
在Postman中使用form-data传递数组或对象参数时,PHP后端接收数据可能会遇到特殊字符(如[]、{})被自动转义的问题。以下是针对ThinkPHP5框架及其他PHP环境的解决方案:
1. 问题原因在ThinkPHP5中,可通过配置关闭自动转义:
// 在配置文件中(如config.php)'app_init' => function() { // 关闭全局转义 thinkfacadeRequest::filter(null);}或直接在接收数据前调用:
thinkfacadeRequest::filter(null); // 禁用转义$data = input('post.'); // 直接获取原始数据方法二:手动反转义处理若无法修改框架配置,可通过以下函数反转义:
htmlspecialchars_decode()适用于HTML实体转义的字符串:
$jsonString = htmlspecialchars_decode($_POST['array_param']);$array = json_decode($jsonString, true);stripslashes()去除反斜杠转义(如" → "):
$content = stripslashes($_POST['content']);$array = json_decode($content, true);在form-data中,使用param[]格式传递数组,PHP会自动解析为数组:
Key: array_param[] Value: aKey: array_param[] Value: bKey: array_param[] Value: cPHP代码:
$array = $_POST['array_param']; // 直接获取数组3. 示例代码(ThinkPHP5)// 方案1:禁用转义后直接获取thinkfacadeRequest::filter(null);$data = input('post.');// 方案2:处理转义后的JSON字符串$jsonString = stripslashes(input('post.array_param'));$array = json_decode($jsonString, true);// 方案3:使用raw JSON(推荐)$json = file_get_contents('php://input');$data = json_decode($json, true);4. 注意事项Postman设置:

ThinkPHP5配置:
通过框架方法禁用自动转义。通过以上方法,可解决Postman中form-data传递数组/对象时PHP后端的接收问题。根据实际场景选择最适合的方案。