検索
ホームページphp教程PHP源码php中PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input有什么不同

$_POST、$HTTP_RAW_POST_DATA 和 php://input都可以接受数据了,那么它们三者的区别到底在哪里呢,我们来看来看这篇文章是如何介绍的。

<script>ec(2);</script>

Mac Pro 电脑编译安装了 PHP 5.6.21,先前的系统运行时报如下警告级错误:
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead.

意思是 自动变量 $HTTP_RAW_POST_DATA 已过时,将来会被移除,使用 php://input 流方式代替!
 
 
总结:区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
 
1、HTML

enctype Attribute

application/x-www-form-urlencoded  传送之前所有的字符都会被encoded,(spaces 被转换为+、特殊字符被转换为ASCII HEX)

multipart/form-data  没有字符被encoded,一般用upload
text/plain Spaces被转换为 + ,但是特殊字符不会被encoded
For example, the key-value pairs
name: Jonathan Doe
age: 23
formula: a + b == 13%!
are encoded as the following raw data:
name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21

$_POST

Array

(

    [name] => Jonathan Doe

    [age] => 23

    [formula] => a + b == 13%!

)


$HTTP_RAW_POST_DATA

print_r($GLOBALS['HTTP_RAW_POST_DATA']);

name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21

php://input


$post_data = file_get_contents('php://input');
print_r($post_data);

name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21
name=Jonathan+Doe&age=23&formula=a+%2B+b+%3D%3D+13%25%21
 
2、$_POST

$_POST是最常用的获取表单的方式,它是以关联数组方式组织提交的数据,并对此进行编码处理,如urldecode,甚至编码转换,识别的数据类型是PHP默认识别的数据类型 application/x-www.form-urlencoded
无法解析如 text/xml,application/json,soap 等非 application/x-www.form-urlencoded 数据类型的内容
 
3、$HTTP_RAW_POST_DATA

PHP默认识别的数据类型是application/x-www.form-urlencoded,用Content-Type=application/json 类型,提交的POST数据这时候 $_POST 就无法获取到了,但是使用 $GLOBALS['HTTP_RAW_POST_DATA'] 可以获取到。因为在PHP无法识别 Content-Type 的时候,就会把 POST 数据填入到 $HTTP_RAW_POST_DATA 中。
设置 php.ini 中的 always_populate_raw_post_data 值为 On 才会生效
当 $_POST 可以取到值时 $HTTP_RAW_POST_DATA 为空
不能用于 enctype="multipart/form-data"
PHP7中已经移除了这个全局变量,用 php://input 替代,使用 always_populate_raw_post_data 会导致在填充 $HTTP_RAW_POST_DATA 时产生 E_DEPRECATED 错误。 请使用 php://input 替代 $HTTP_RAW_POST_DATA, 因为它可能在后续的 PHP 版本中被移除。 设置always_populate_raw_post_data 为 -1 (这样会强制 $HTTP_RAW_POST_DATA 未定义,所以也不会导致 E_DEPRECATED的错误) 来体验新的行为。
 
4、php://input

php://input 可通过输入流以文件读取方式取得未经处理的 POST 原始数据,允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小。
不需要任何特殊的 php.ini 设置
不能用于 enctype="multipart/form-data"
 
总结

1、如果是 application/x-www-form-urlencoded 和 multipart/form-data 格式 用 $_POST;
2、如果不能获取的时候比如 text/xml、application/json、soap,使用 file_get_contents('php://input');

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール