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

PhpJsonXmlhttprequest

Php Problem Overview


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: https://stackoverflow.com/questions/813487/how-to-post-json-to-php-with-curl/813512#813512

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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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'));

Solution 4 - Php

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

Solution 5 - Php

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.

Solution 6 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManuel BittoView Question on Stackoverflow
Solution 1 - PhpzafView Answer on Stackoverflow
Solution 2 - PhpZeeshan HyderView Answer on Stackoverflow
Solution 3 - PhpzloctbView Answer on Stackoverflow
Solution 4 - PhpZZ CoderView Answer on Stackoverflow
Solution 5 - PhpMarc BView Answer on Stackoverflow
Solution 6 - PhpPekkaView Answer on Stackoverflow