133

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.

Boolean_Type
  • 1,073
  • 3
  • 13
  • 36
Manuel Bitto
  • 4,945
  • 6
  • 38
  • 46

6 Answers6

217

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".

Ahmed Shaqanbi
  • 355
  • 2
  • 9
zaf
  • 22,320
  • 11
  • 63
  • 95
19

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.

Source: http://php.net/manual/en/wrappers.php.php.

kjhughes
  • 98,039
  • 18
  • 159
  • 218
Zeeshan Hyder
  • 510
  • 5
  • 14
15

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 some example :

send by post JSON string

<input type="button" value= "click" onclick="fn()">
<script>
 function fn(){


    var js_obj = {plugin: 'jquery-json', version: 2.3};

    var encoded = JSON.stringify( js_obj );

var data= encoded


    $.ajax({
  type: "POST",
  url: '1.php',
  data: data,
  success: function(data){
    console.log(data);
  }

});

    }
</script>

1.php

//print_r($_POST); //empty!!! don't work ... 
var_dump( file_get_contents('php://input'));
zloctb
  • 9,536
  • 6
  • 64
  • 82
4

For JSON data, it's much easier to POST it as "application/json" content-type. If you use GET, you have to URL-encode the JSON in a parameter and it's kind of messy. Also, there is no size limit when you do POST. GET's size if very limited (4K at most).

ZZ Coder
  • 72,880
  • 29
  • 134
  • 165
  • 3
    There often is a size limit for POST, but it's usually set pretty high. Check your `php.ini`. – Brad Jul 23 '14 at 19:30
3

The usual rules should apply for how you send the request. If the request is to retrieve information (e.g. a partial search 'hint' result, or a new page to be displayed, etc...) you can use GET. If the data being sent is part of a request to change something (update a database, delete a record, etc..) then use POST.

Server-side, there's no reason to use the raw input, unless you want to grab the entire post/get data block in a single go. You can retrieve the specific information you want via the _GET/_POST arrays as usual. AJAX libraries such as MooTools/jQuery will handle the hard part of doing the actual AJAX calls and encoding form data into appropriate formats for you.

Marc B
  • 348,685
  • 41
  • 398
  • 480
  • That's the point: i want to grab the entire post/get data block in a single go, because JSON is a variable-less format, it rapresent just the data. – Manuel Bitto Apr 28 '10 at 16:32
  • @Kucebe I don't see why this is necessary, why not put the JSON data into a POST field and be done with it? – Pekka Apr 28 '10 at 16:36
  • If you want the entire JSON block, then why not assign the JSON text block to a form field and submit it like that? `` is entirely acceptable and lets you retrieve it trivially server-side with $_REQUEST['data']. – Marc B Apr 28 '10 at 18:59
  • 3
    Embedding JSON in a POST field defeats the purpose of the HTTP content-type tag, and is not as nice for debugging in Fiddler and browser debuggers (which can understand JSON). Also, many 3rd party JavaScript libraries POST JSON payloads as application/json. – CyberMonk May 23 '13 at 19:58
2

Your second question is easy, GET has a size limitation of 1-2 kilobytes on both the server and browser side, so any kind of larger amounts of data you'd have to send through POST.

halfer
  • 19,471
  • 17
  • 87
  • 173
Pekka
  • 431,103
  • 135
  • 960
  • 1,075