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? php php

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


Actually php://input allows you to read raw POST data.

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

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

Reference: http://php.net/manual/en/wrappers.php.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.


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