Search code examples
phpjsonxmlhttprequest

file_get_contents("php://input") or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?


file_get_contents("php://input") or $HTTP_RAW_POST_DATA - which one is better to get the body of JSON request?

And which request type (GET or POST) should I use to send JSON data when using client side XmlHTTPRequest?

My question was inspired from this answer: How to post JSON to PHP with curl

Quote from that answer:

From a protocol perspective file_get_contents("php://input") is actually more correct, since you're not really processing http multipart form data anyway.


Solution

  • Actually php://input allows you to read raw request body.

    It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. From Reference

    php://input is not available with enctype="multipart/form-data".