137

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,146
  • 3
  • 13
  • 40
Manuel Bitto
  • 5,073
  • 6
  • 39
  • 47

6 Answers6

223

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
  • 525
  • 5
  • 15
zaf
  • 22,776
  • 12
  • 65
  • 95
20

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
  • 106,133
  • 27
  • 181
  • 240
Zeeshan Hyder
  • 520
  • 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
  • 10,592
  • 8
  • 70
  • 89
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
  • 74,484
  • 29
  • 137
  • 169
  • 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
  • 356,200
  • 43
  • 426
  • 500
  • 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,824
  • 17
  • 99
  • 186
Pekka
  • 442,112
  • 142
  • 972
  • 1,088