0

I am trying to declare HTTP PUT variable in php. This is my code:

<?php 
    ${"_" . $_SERVER['REQUEST_METHOD']} = /* What should be here? */;
?>

I tried var_dump($_SERVER) but it does not contain the data sent using ajax request. I am sure there is no problem with $.ajax().

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • 1
    Dear downvoter. Did you even understand what is my question? –  Feb 05 '18 at 08:47
  • _“I tried var_dump($_SERVER) but it does not contain the data sent using ajax request”_ - and why should it …? https://lornajane.net/posts/2008/accessing-incoming-put-data-from-php, http://php.net/manual/en/features.file-upload.put-method.php – CBroe Feb 05 '18 at 08:47
  • take deeplook @ http://php.net/manual/en/features.file-upload.put-method.php – TarangP Feb 05 '18 at 08:48
  • 2
    @CBroe Atleast the question is better than *I am trying to run this code but it gives me error undefined variable*, right? – Aniket Sahrawat Feb 05 '18 at 08:49
  • Check out this question on how to identify put requests in PHP and how to get the request content: https://stackoverflow.com/questions/359047/detecting-request-type-in-php-get-post-put-or-delete – Erik Kalkoken Feb 05 '18 at 08:52
  • Also, you question is not valid PHP code. I recommend learning about the syntax on this great [tutorial](https://www.w3schools.com/php/default.asp). – Erik Kalkoken Feb 05 '18 at 08:53
  • 2
    @ErikKalkoken It is completely valid, try `${"_PUT"}="hi"; var_dump($_PUT);` –  Feb 05 '18 at 08:54
  • @ErikKalkoken There is no problem figuring what is the request type, i am concerned about the data sent with it. I am trying to create `$_PUT` as `$_GET`. –  Feb 05 '18 at 08:55
  • yes, I see it know. Apparently need more coffee ;-) – Erik Kalkoken Feb 05 '18 at 08:55
  • 2
    @ErikKalkoken I understand, you were probably judging me on based of reputation. Rep is just a number :) Anyways, have a nice day :) –  Feb 05 '18 at 09:00
  • Possible duplicate of [HTTP protocol's PUT and DELETE and their usage in PHP](https://stackoverflow.com/questions/27941207/http-protocols-put-and-delete-and-their-usage-in-php) – JustCarty Feb 05 '18 at 09:04
  • @CBroe For treating badly ;) –  Feb 05 '18 at 09:08

1 Answers1

5

While there is no official $_PUT variable in PHP, you can create one yourself like this:

$method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method) {
    parse_str(file_get_contents('php://input'), $_PUT);
    var_dump($_PUT); //$_PUT contains put fields 
}

Source: https://stackoverflow.com/a/41959141/4379151

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114