Get PATCH request data in PHP

前端 未结 4 1487
無奈伤痛
無奈伤痛 2020-12-16 12:08

I need to make a PATCH request to a PHP application.

How can I get the data from that PATCH request inside that application?

If I h

相关标签:
4条回答
  • 2020-12-16 12:42

    You have $_REQUEST superglobal containing all data we can get regardless the HTTP method used (GET, POST, PATCH, PUT)

    0 讨论(0)
  • 2020-12-16 12:49

    Since none of the above has worked for me in PHP 5.6, here's a solution that actually did.

    I used this parse_raw_http_request($data) function by Christof.

    And here's the code:

    $_PATCH = [];
    parse_str(file_get_contents('php://input'), $_PATCH);
    parse_raw_http_request($_PATCH);
    
    // From now on, the $_PATCH variable keeps all request arguments as well,
    // and they're accessible under approprate keys like $_PATCH['yourKey']
    
    0 讨论(0)
  • 2020-12-16 12:50

    I know that this has been solved, but for anyone who was hoping for an answer like

    $_PATCH["name"];
    

    there is a way to do that:

    parse_str(file_get_contents('php://input'), $_PATCH);
    

    then you can access it like $_GET["something"] and $_POST["something"] just do

    $_PATCH["something"]
    

    hope that helped someone :)

    0 讨论(0)
  • 2020-12-16 12:58

    You can get data with php://input stream wrapper:

    $data = file_get_contents('php://input');
    

    Also make sure your web server supports PATCH requests, some are configured to respond only to GET and POST.

    0 讨论(0)
提交回复
热议问题