Get PUT params with Slim PHP

若如初见. 提交于 2019-12-01 14:51:04

问题


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:

URL

http://localhost/api/update/987654321

Params

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')
    ]);
});

My var_dump() result is:

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

What is wrong? Any idea?


回答1:


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.

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.

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).




回答2:


$app->request->put() is returning a null value...

so u can use try $app->request->params instead



来源:https://stackoverflow.com/questions/23761425/get-put-params-with-slim-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!