使用 Slim PHP 获取 PUT 参数 [英] Get PUT params with Slim PHP

查看:68
本文介绍了使用 Slim PHP 获取 PUT 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I searched, but I didn't find an answer. I have a RESTful API to manage a basic CRUD. I'm trying to create an update method using PUT, but I can't retrieve the params values. I'm using Postman to make the requests, my request looks like:

网址

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
}

怎么了?有什么想法吗?

What is wrong? Any idea?

推荐答案

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

I had the same problem. Firstly, I was sending PUT data with the Postman option to encode it as "form-data", that's why Slim wasn't getting the param values.

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

As it is explained in W3, the content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

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

In our case, we have to send PUT data with the Postman option "x-www-form-urlencoded" (see explanation of "application/x-www-form-urlencoded" in W3).

这篇关于使用 Slim PHP 获取 PUT 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆