Get PATCH request data in PHP

Viewed 19261

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 had to do it with a POST, it would just be a simple access to the global $_POST variable.

5 Answers

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']

i'm using : PHP 7.4

function patchMethod(){
      parse_str(file_get_contents('php://input'), $_PATCH);
              $body=[];
                if (is_array($_PATCH)) {
                   foreach ($_PATCH as $key => $value) {
                    $body[$key] = $value;
                }  
                   
                }
            return $body;    
               
}
Related