English 中文(简体)
文档_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 http://input” or $HTTP_RAW_POST_DATA - which one is better to have the Body of JSON request?

哪一类请求(GETPOST)?

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

回答:

从礼宾角度看,file_get_contents("php http://input”)实际上更加正确,因为你没有真正处理http:// multipart form data noway。

最佳回答

实际上php http://input 允许你阅读原始申请机构。

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

<代码>php http://input 不能在enctype=“multipart/form-data>上查阅。

问题回答

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.

资料来源:。 http://php.net/manual/en/wrappers.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 :

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

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

对于JSON数据,PPOST作为“应用/json”内容类型更容易。 如果你使用GET,你就不得不将JSON编成一个参数,并用一种幻觉。 而且,当你做《人口调查》时,不存在任何规模限制。 GET的面积如果非常有限(多数为4K)。

通常的规则应当适用于你如何发出请求。 如果要求收回信息(如部分搜寻结果,或显示的新页等),你可以使用GET。 如果所发送的数据是更改某种内容(更新数据库、删除记录等)的请求的一部分,则使用《名册》。

服务器方面,没有理由使用原始投入,除非你想要在单行中挖掘整个员额/植被数据组。 您可以按照惯例通过_GET/_POST阵列获取你想要的具体信息。 复兴共和与民主军的图书馆,如MooTools/jQuery,将处理实际的复兴共和与民主军的电话和编码数据输入适当的格式的硬部分。

你的第二个问题是轻而易举的,在服务器和浏览器方面,GET的大小限制是1-2千兆字节,因此,你必须通过POST发送的任何种类更多的数据。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签