Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用slimphp获取PUT参数_Php_Rest_Put_Restful Url_Slim - Fatal编程技术网

使用slimphp获取PUT参数

使用slimphp获取PUT参数,php,rest,put,restful-url,slim,Php,Rest,Put,Restful Url,Slim,我搜索了一下,但没有找到答案。我有一个RESTful API来管理一个基本的CRUD。我正在尝试使用PUT创建更新方法,但无法检索参数值。我正在使用发出请求,我的请求如下所示: URL http://localhost/api/update/987654321 参数 id = 987654321 name = John Smith age = 35 PHP $app = new Slim(); $app->put('/update/:id', function( $id ) use(

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

URL

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\u dump()
结果是:

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

怎么了?有什么想法吗?

我也有同样的问题。首先,我使用Postman选项发送PUT数据,将其编码为“表单数据”,这就是为什么Slim无法获取param值的原因

如中所述,内容类型“多部分/表单数据”应用于提交包含文件、非ASCII数据和二进制数据的表单

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

$app->request->put()
正在返回空值


因此,如果您向下滚动到请求变量部分,您可以使用try
$app->request->params
来检查手册,这里有一个示例。或者,您可以直接从主体获取参数,并通过执行
parse\u str(file\u get\u contents(“php://input“,$post_vars)我想在此请求中上载图像。使用
POST
更新是一种糟糕的做法?很抱歉,我的评论不是关于
POST
数据的,我是从另一个站点复制代码的。但是,PUT数据是在请求体中设置的,就像GET之外的所有其他类型的请求一样(尽管您可能能够将一个正文字符串附加到GET请求,但我从未尝试过)。你可以做
parse_str($app->request->getBody(),$vars)并在其上进行var_转储以查看您的数据。切换到x-www-form-urlencoded with postman以使其正常工作!谢谢你解释得很好的答案(w3链接)!你刚刚帮我省去了几个小时的头痛。非常感谢。我有一个问题,用x-www-form-urlencoded无法上传文件,它只接受字符串。如何使用PUT/PATCH方法上载文件。。?