4

Is there any way I can access data that was sent via HTTP PUT method other than $putdata = fopen("php://input", "r");?

I have never worked with PUT and DELETE methods and $putdata = fopen("php://input", "r"); seems a bit sketchy. Will it work everywhere is a specific server/php.ini configuration required?

I know that I can get the request method from $_SERVER['REQUEST_METHOD'];

But will the data be in $_REQUEST, if yes then what php://input is about? And how do I access data that was sent via DELETE?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rinchik
  • 2,642
  • 8
  • 29
  • 46

1 Answers1

3

No, you will need to parse the request manually. $_REQUEST only contains data coming from GET and POST requests; for everything else you are on your own.

If your HTTP request has Content-Type: application/x-www-form-urlencoded, you can parse it back into a variables array very easily with parse_str like this:

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

You can use this content type with any HTTP method, there is no standard-imposed limitation.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Interesting! And 'DELETE' will also be in `php://input`? – rinchik Apr 30 '13 at 21:28
  • @rinchik: `php://input` contains whatever the client sent as the body of the HTTP request. The method (GET/POST/DELETE/whatever) is also the client's choice. The type of data sent as the body is also the client's choice. So yes, it will work for everything *as long as the server knows how to process what the client sent*. My example illustrates only one possible way of doing things, chosen because it's exactly the same as what's used when a form is submitted with POST. – Jon Apr 30 '13 at 21:32
  • oh! just found this: http://uk.php.net/manual/en/function.http-get-request-body-stream.php alternative to `file_get_contents('php://input'), $vars` – rinchik Apr 30 '13 at 22:01
  • 1
    @rinchik: I don't see the point in using a function from an extension you have to install manually instead of a built-in that does the same job. – Jon Apr 30 '13 at 22:03