0

For the get request we can convert the query string to string using,

parse_str($_SERVER['QUERY_STRING']);

But for the post method above method don't work. Is there another method which will make the post query to the string.

abhishek bagul
  • 243
  • 2
  • 16

1 Answers1

1

Unless you are running PHP from the command line, PHP will populate the superglobal arrays $_GET and $_POST (among others) with the parsed query values and respective post body values. It will do that automatically, so there is no reason why you would parse_str on the query string like you show.

Quoting the PHP Manual for $_POST

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

This is the prefered way of accessing POST data. However, you can also get access to the raw post body, via the php://input stream or $HTTP_RAW_POST_DATA (the latter is deprecated in PHP 5.6.0, and REMOVED as of PHP 7.0.0).

Quoting the PHP Manual on input streams:

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559