【问题标题】:How to handle PUT requests in RESTful API?如何在 RESTful API 中处理 PUT 请求?
【发布时间】:2015-02-09 11:02:29
【问题描述】:

我想在我的 Web 服务中使用 PUT 请求进行更新操作,但我不知道如何处理 PUT,因为没有像 $_PUT 这样的内置变量。

我试过parse_str(file_get_contents("php://input"),$post_vars); 现在如果我print_r$post_vars,它正在显示

------WebKitFormBoundaryJnBh3L2DKmMjdVmG Content-Disposition: form-data; name="fruit" vanana ------WebKitFormBoundaryJnBh3L2DKmMjdVmG Content-Disposition: form-data; name="items" 1 ------WebKitFormBoundaryJnBh3L2DKmMjdVmG--

我如何才能获得在发出PUT 请求时传递的确切参数?

【问题讨论】:

    标签: php request put http-method


    【解决方案1】:

    你的问题

    您的 PUT 请求具有 multipart/form-data 内容类型。 所以parse_str函数无法解析它。

    解决方案

    您必须使用Content-Type=application/x-www-form-urlencoded 提出 PUT 请求。然后就可以用parse_str(file_get_contents("php://input"), $post_vars);解析了

    注意事项

    您将无法上传文件。因为文件需要使用multipart/form-data

    【讨论】:

      【解决方案2】:

      有一个超级全局$_SERVER 包含REQUEST_METHOD

      可用于标识访问页面的请求方法;即“GET”、“HEAD”、“POST”、“PUT”。

      所以你可以在你的服务器上使用它来识别 put 请求。

      【讨论】:

      • 我想从那个 PUT 请求中获取参数和它的值。正如您在我的帖子中看到的那样,我有一个名为“fruit”的参数,值为“vanana”,“items”的值为“1”。我想要这个参数。
      • 您可以从 $post_vars 中访问这些参数及其值,例如 $post_vars['fruit'] $post_vars['items']
      • 注意:未定义索引:F:\xampp\htdocs\tempput.php 中的水果在第 14 行这是错误!
      猜你喜欢
      • 2021-01-29
      • 2017-04-03
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多