简体   繁体   English

file_get_contents(“php:// input”)或$ HTTP_RAW_POST_DATA,哪一个更好地获取JSON请求的主体?

[英]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? file_get_contents("php://input")$HTTP_RAW_POST_DATA - 哪一个更好地获取JSON请求的主体?

And which request type ( GET or POST ) should I use to send JSON data when using client side XmlHTTPRequest ? 在使用客户端XmlHTTPRequest时,我应该使用哪种请求类型( GETPOST )来发送JSON数据?

My question was inspired from this answer: How to post JSON to PHP with curl 我的问题来自这个答案: 如何使用curl将JSON发布到PHP

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. 从协议的角度来看, file_get_contents("php://input")实际上更正确,因为你还没有真正处理http多部分表单数据。

Actually php://input allows you to read raw POST data. 实际上php://input允许您读取原始POST数据。

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives . 它是$ HTTP_RAW_POST_DATA的内存密集型替代品,不需要任何特殊的php.ini指令

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

Reference: http://php.net/manual/en/wrappers.php.php 参考: 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. php:// input是一个只读流,允许您从请求正文中读取原始数据。 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 . 在POST请求的情况下,最好使用php:// input而不是$ HTTP_RAW_POST_DATA,因为它不依赖于特殊的php.ini指令 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. 此外,对于默认情况下未填充$ HTTP_RAW_POST_DATA的情况,它可能是激活always_populate_raw_post_data的内存密集型替代方案。

Source: http://php.net/manual/en/wrappers.php.php . 资料来源: 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 PHP some example : file_get_contents(php:// input) - 获取原始POST数据,你需要在编写API时使用它,并且需要XML / JSON / ...输入,这些输入无法通过PHP解码为$ _POST一些例子:

send by post JSON string 通过邮寄JSON字符串发送

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

//print_r($_POST); //empty!!! don't work ... 
var_dump( file_get_contents('php://input'));

The usual rules should apply for how you send the request. 通常的规则应适用于您发送请求的方式。 If the request is to retrieve information (eg a partial search 'hint' result, or a new page to be displayed, etc...) you can use GET. 如果请求是检索信息(例如,部分搜索'提示'结果,或要显示的新页面等等),您可以使用GET。 If the data being sent is part of a request to change something (update a database, delete a record, etc..) then use POST. 如果发送的数据是更改内容的请求(更新数据库,删除记录等等)的一部分,则使用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. 服务器端,没有理由使用原始输入,除非你想一次性获取整个post / get数据块。 You can retrieve the specific information you want via the _GET/_POST arrays as usual. 您可以像往常一样通过_GET / _POST数组检索所需的特定信息。 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. 诸如MooTools / jQuery之类的AJAX库将处理执行实际AJAX调用和将表单数据编码为适当格式的困难部分。

第二个问题很简单,GET在服务器端和浏览器端的大小限制为1-2千字节,因此您必须通过POST发送任何类型的大量数据。

For JSON data, it's much easier to POST it as "application/json" content-type. 对于JSON数据,将它作为“application / json”内容类型发布要容易得多。 If you use GET, you have to URL-encode the JSON in a parameter and it's kind of messy. 如果你使用GET,你必须在参数中对JSON进行URL编码,这有点混乱。 Also, there is no size limit when you do POST. 此外,POST时没有大小限制。 GET's size if very limited (4K at most). GET的大小如果非常有限(最多4K)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有$ GLOBALS [“HTTP_RAW_POST_DATA”]的file_get_contents($ filename)来获取文件名? - How to use file_get_contents($filename) with $GLOBALS[“HTTP_RAW_POST_DATA”] to get filename? 使用CURL而不使用file_get_contents从php:// input获取/读取原始帖子数据 - get/read raw post data from php://input using CURL and not using file_get_contents 无法使用file_get_contents(&#39;php:// input&#39;)检索原始帖子数据 - can't retrive raw post data using file_get_contents('php://input') 如何让Jquery发送POST数据来填充PHP中的$ HTTP_RAW_POST_DATA var? - how to get Jquery to send POST data to populate $HTTP_RAW_POST_DATA var in PHP? 在HTTP_RAW_POST_DATA上进行输入验证 - input validation on HTTP_RAW_POST_DATA 我通过 postman 将原始 JSON 数据发送到 PHP ZC1C425268E68385D1AB507ZUTC14(A 方法)。 file_get_contents('php://input') 给出 null - I am sending raw JSON data through postman to a PHP function (PUT method). file_get_contents('php://input') gives null file()VS file_get_contents()哪个更好? PHP - file() VS file_get_contents() which is better? PHP 需要带有file_get_contents(PHP)的HTTP 500的响应体 - Need response body of HTTP 500 with file_get_contents (PHP) 如果POST正文超过〜16 000个字符,则file_get_contents(“ php:// input”)为空 - file_get_contents(“php://input”) is empty if the POST body is above ~16 000 characters 使用 file_get_contents 发出 POST 应用程序/json 请求 - Making POST application/json request with file_get_contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM