Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_POST vs. $HTTP_RAW_POST_DATA vs file_get_contents(php://input)? [duplicate]

Tags:

php

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?

like image 865
Michael Avatar asked Oct 21 '12 10:10

Michael


1 Answers

  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.

like image 178
CodeAngry Avatar answered Nov 16 '22 00:11

CodeAngry