21

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.

Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79

5 Answers5

33

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 :)

MeepMu
  • 475
  • 5
  • 6
  • Wait, that doesn't work. I get keys something like `------WebKitFormBoundaryDBQQ4THK5a0cbY20 Content-Disposition:_form-data;_name` – Lis Oct 16 '20 at 06:56
  • Alright, I just realized this will probably not work correctly in PHP versions < 7 – Lis Oct 16 '20 at 07:06
  • Not even working in `PHP 7.2.34` getting same `----------------------------541429037887217943792898 Content-Disposition:_form-data;_name] => "key"` – santoshe61 Feb 10 '21 at 17:37
  • I don't think [`parse_str()`](https://www.php.net/manual/en/function.parse-str.php) can parse `multipart/form-data`. That's why you get boundary strings. – akinuri May 15 '21 at 20:00
26

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.

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • but how to get use that data as simple like `$_GET or $_POST` ..... it is returning some weird string – santoshe61 Feb 10 '21 at 17:39
  • @santosh It's not that simple. You should also check Content-Type request header and decide what to do with this string. It can be JSON, base64 encoded file or urlencoded query string. – dev-null-dweller Feb 10 '21 at 22:29
0

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']
Lis
  • 555
  • 4
  • 26
0

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;    
               
}
kadiro
  • 956
  • 1
  • 10
  • 16
-7

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

Matthias Seifert
  • 2,033
  • 3
  • 29
  • 41
neby55
  • 1
  • 5
    No, you don't: `An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.` – danielMitD Feb 19 '18 at 20:46