15

Possible Duplicate:
What’s the difference between POST and raw POST in PHP at all?

For a better understanding, I would be grateful if you would explain what are the fundamental differences between $_POST, $HTTP_RAW_POST_DATA and file_get_contents(php://input).

When to use which, and why?

Community
  • 1
  • 1
Michael
  • 4,786
  • 11
  • 45
  • 68
  • 4
    Have you looked at the manual? What's unclear specifically? – mario Oct 21 '12 at 11:00
  • I belive that 90% of the php programmers user $_POST variable in order to get their posted data. I am working at a project where I have to process a response from a service provider and with $_POST I couldn't reach that piece of data. I don't know exactly why, and I want a clear view over this. I took a look at the manual but still don't have a good understanding. – Michael Oct 21 '12 at 11:05
  • I will go through the link you suggested, mario. – Michael Oct 21 '12 at 11:06
  • hi michael, that looks like a question from a test? Is it? – Toby Allen Oct 21 '12 at 11:06
  • If you are having trouble accessing data try using [`var_dump()`](http://php.net/manual/en/function.var-dump.php) to see what values are there. – PeeHaa Oct 21 '12 at 11:07
  • As PeeHaa said, but also look at `var_dump($_SERVER);` then to see the type of POST paylod by inspecting `CONTENT_TYPE`. – mario Oct 21 '12 at 11:08
  • PeeHaa, Thank you! I am familiar with var dumping and debugging in general. I also have Zend_Debug methods in this particular framework. What I want is a fundamental understanding. – Michael Oct 21 '12 at 11:09
  • @mario, [this](http://stackoverflow.com/questions/3173547/whats-the-difference-between-post-and-raw-post-in-php-at-all) cleared things up. Thank you! Sorry for missing it! – Michael Oct 21 '12 at 11:18

1 Answers1

28
  1. $_POST contains URL encoded (application/www-url-encoded) variables that are posted to your script and PHP decodes them for you. You use this one when you deal with HTML FORM data.
  2. file_get_contents("php://input") - gets the raw POST data and you need to use this when you write APIs and need XML/JSON/... input that cannot be decoded to $_POST by PHP.
  3. $HTTP_RAW_POST_DATA - in theory it is the same as the above but depends on php.ini. (DEPRECATED, SEE COMMENT)

I always use method #2 instead of #3 when I need non application/www-url-encoded input.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • `$HTTP_RAW_POST_DATA` was [deprecated in PHP 5.6](https://www.php.net/manual/en/migration56.deprecated.php) – Ian Dunn May 07 '22 at 15:25