file_get_contents("php://input") 或 $HTTP_RAW_POST_DATA,哪个更适合获取 JSON 请求的正文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2731297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:30:14  来源:igfitidea点击:

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

phpjsonxmlhttprequest

提问by Manuel Bitto

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 (GETor POST) should I use to send JSON data when using client side XmlHTTPRequest?

在使用客户端时,我应该使用哪种请求类型(GETPOST)来发送 JSON 数据XmlHTTPRequest

My question was inspired from this answer: How to post JSON to PHP with curl

我的问题来自这个答案: 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.

从协议的角度来看file_get_contents("php://input"),实际上更正确,因为无论如何您都没有真正处理 http 多部分表单数据。

回答by zaf

Actually php://inputallows 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://inputis 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

回答by Zeeshan Hyder

php://input is a read-onlystream 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.

php://input 是一个只读流,允许您从请求正文中读取原始数据。在 POST 请求的情况下,最好使用 php://input 而不是 $HTTP_RAW_POST_DATA 因为它不依赖于特殊的 php.ini 指令。此外,对于默认情况下未填充 $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

回答by zloctb

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 :

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

回答by Marc B

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.

通常的规则应该适用于您发送请求的方式。如果请求是检索信息(例如部分搜索“提示”结果,或要显示的新页面等...),您可以使用 GET。如果发送的数据是更改某些内容(更新数据库、删除记录等)请求的一部分,则使用 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.

服务器端,没有理由使用原始输入,除非您想一次性获取整个 post/get 数据块。您可以像往常一样通过 _GET/_POST 数组检索所需的特定信息。MooTools/jQuery 等 AJAX 库将处理执行实际 AJAX 调用和将表单数据编码为适合您的适当格式的困难部分。

回答by ZZ Coder

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

对于 JSON 数据,将其发布为“application/json”内容类型要容易得多。如果使用 GET,则必须在参数中对 JSON 进行 URL 编码,这有点麻烦。此外,在执行 POST 时没有大小限制。GET 的大小如果非常有限(最多 4K)。

回答by Pekka

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.

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