【问题标题】:How to get the data from PUT HTTP Request (form-data)如何从 PUT HTTP 请求(表单数据)中获取数据
【发布时间】:2016-02-25 13:58:54
【问题描述】:

这是我的 API 中的函数,我需要从 HTTP 请求的表单数据中获取数据。

我需要获取 put 值来更新我的类查询中的数据。

    function method()
    {
        $method = $_SERVER['REQUEST_METHOD'];

        return $method;
    }

    function updateUser()
    {
        $method = method();

        if($method == 'PUT'){

            $put_data = file_get_contents("php://input");

            parse_str($put_data, $post_vars);


            return $post_vars;


        }else{

            $status = '400 Bad Request';

            return $status;

        }
     }

{"-----WebKitFormBoundaryYKcobRh4FtrGCYaI\r\nContent-Disposition:_form-data;_name":"\"test\"\r\n\r\ntest_value\r\n------ WebKitFormBoundaryYKcobRh4FtrGCYaI--\r\n"}

这是我从 return $post_vars 中得到的

我需要测试:test_value

【问题讨论】:

    标签: php api rest httprequest put


    【解决方案1】:

    将请求的编码从 multipart/form-data 更改为 application/x-www-form-urlencoded

    【讨论】:

      【解决方案2】:

      尝试替换

          $method = method();
      
              if($method == 'PUT'){
      //dosomething
      }else{
      //error
      }
      

         if (!($putData = fopen("php://input", "r"))){ 
                      throw new Exception("Can't get PUT data."); }
      else{ //dostuff 
      }
      

      有关更多信息,请参阅: http://php.net/manual/en/features.file-upload.put-method.php

      【讨论】:

      • if (!($putData = fopen("php://input", "r"))){ throw new Exception("Can't get PUT data."); }else{ $id = $putData['id'];返回$id;感谢它确实有效,但返回 null :(
      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2013-11-01
      • 2013-08-03
      • 2017-10-24
      • 2018-08-26
      相关资源
      最近更新 更多