gpt4 book ai didi

php - 使用 Slim PHP 获取 PUT 参数

转载 作者:IT王子 更新时间:2023-10-29 00:12:13 24 4
gpt4 key购买 nike

我搜索过,但没有找到答案。我有一个 RESTful API 来管理基本的 CRUD。我正在尝试使用 PUT 创建更新方法,但无法检索参数值。我正在使用 Postman要发出请求,我的请求如下所示:

网址

http://localhost/api/update/987654321

参数

id = 987654321
name = John Smith
age = 35

PHP

$app = new Slim();
$app->put('/update/:id', function( $id ) use( $app ){
var_dump([
'id' => $id,
'name' => $app->request->put('name'),
'age' => $app->request->put('age')
]);
});

我的 var_dump() 结果是:

array(3) {
["id"]=>
string(9) "987654321"
["name"]=>
NULL
["age"]=>
NULL
}

怎么了?有什么想法吗?

最佳答案

我遇到了同样的问题。首先,我使用 Postman 选项发送 PUT 数据以将其编码为“表单数据”,这就是 Slim 未获取参数值的原因。

W3 中所述,内容类型“multipart/form-data”应用于提交包含文件、非 ASCII 数据和二进制数据的表单。

在我们的例子中,我们必须使用 Postman 选项“x-www-form-urlencoded”发送 PUT 数据(参见 W3 中“application/x-www-form-urlencoded”的解释)。

Screenshot of the right Postman option selected

关于php - 使用 Slim PHP 获取 PUT 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23761425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com