简体   繁体   English

使用Slim框架的JSON PUT请求

[英]JSON PUT request using Slim framework

I am trying to make a PUT request to Slim using the following code: 我正在尝试使用以下代码向Slim发出PUT请求:

 <script type = "text/javascript">
 function submitform() {
   var url = '/users/' + $('#user_id').val();
   $('#myform').attr('action', url);
   var data = JSON.stringify({"value": $('#user_data').val()});
   $('<input type="hidden" name="json"/>').val(data).appendTo('#myform');
   $("#myform").submit();
   }
 </script>

 <form id = "myform" method="post">
 id:   <input type = "text" id = "user_id">
 data: <input type = "text" name = "value" id = "user_data">
 <input type="hidden" name="_METHOD" value="PUT"/>
 <input type = "button" value = "submit" onClick='submitform()'>
 </form>

My index.php file contains: 我的index.php文件包含:

$app->put('/users/:id', 'update');
function update($id) {
  $jsonmessage = \Slim\Slim::getInstance()->request();
  $message = json_decode($jsonmessage->getBody());
  // what do I put here ????
  }

What should I put in place of the ???? 我该怎么代替???? to retrieve the value parameter. 检索值参数。 I can see it if I use $_POST['json'] but I don't think this is REST compliant. 如果我使用$ _POST ['json']我可以看到它,但我认为这不符合REST。 Everything else I've tried, such as $message->value, doesn't work (returns nothing). 我尝试过的其他所有内容,例如$ message-> value,都不起作用(不返回任何内容)。

Thanks. 谢谢。

UPDATE: 更新:

Somebody from the Slim forum provided the following answer: 来自Slim论坛的人提供了以下答案:

$json = $jsonmessage->put('json');
$message = json_decode($json);

If you want to capture the parameter of your JSON put request, then there is a simple solution in Slim. 如果你想捕获你的JSON put请求的参数,那么Slim中有一个简单的解决方案。

//$app->request()->params('parameterKey');
$params = $app->request()->params('value');

I think I got your question and let me know if it solves. 我想我得到了你的问题,让我知道它是否解决了。

$message->value $ MESSAGE->值

json_decode will create a standard php object for you. json_decode将为您创建一个标准的php对象。 You can also try var_dump($message) to see the structure of the object for debugging. 您还可以尝试使用var_dump($ message)来查看对象的结构以进行调试。

I use the below code to pull the request body 我使用下面的代码来拉取请求体

$request = Slim::getInstance()->request();
$user = json_decode($request->getBody());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM