douduanque5850
douduanque5850
2016-02-25 13:58

如何从PUT HTTP请求(表单数据)获取数据

已采纳

This is my function in my API I need to get the data from the form-data from the HTTP request.

I need to get the put values to update my data in my class query.

    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 Content-Disposition:_form-data;_name":"\"test\" test_value ------WebKitFormBoundaryYKcobRh4FtrGCYaI-- "}

This is what i get from the return $post_vars

I need to have test: test_value

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douwen1901 douwen1901 5年前

    Change the encoding of the request from multipart/form-data to application/x-www-form-urlencoded

    点赞 评论 复制链接分享
  • doukong6031 doukong6031 5年前

    Try replacing

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

    With

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

    for more information see: http://php.net/manual/en/features.file-upload.put-method.php

    点赞 评论 复制链接分享

相关推荐