PHP 未记录自动解码内容类型:application/json 到 $_REQUEST
PHP undocumented automatically decode content-type:application/json to $_REQUEST
PHP 中真的有一个未记录的功能来将 JSON POST 正文解码为 $_REQUEST 吗?
不等于PHP is automatically decoding JSON data?
我正在 PHP 中创建一个 API 以接收 FormData 和 JSON 中的参数。当我在 POSTMAN 中测试时,似乎 PHP 自动将带有 content-type:application/json
的 POST 主体解码为 $_REQUEST
。
我整个下午都在测试它,不知道怎么可能。
这是 post 请求:
curl -X POST \
http://xxxxx.com/api/user/profile/update \
-H 'content-type: application/json' \
-d '{
"id" : 174,
"email" : "reed@xxxxx.com",
"lang": "en_US",
}'
这是我在PHP
中得到的
var_dump($_REQUEST)
//output
'id' => int 174
'email' => string 'reed@xxxxx.com' (length=19)
'lang' => string 'en_US' (length=5)
我看到的是错觉吗??
如果我将 POST 内容类型更改为任何非 application/json
的内容,则不会发生自动解码。
我正在使用 PHP5.6 + Phalcon 3.1。两者都没有提及自动解码 JSON.
这是我的 composer.json
,我认为这些软件包中的任何一个都不会做这样的事情。
"require": {
"twilio/sdk" : "4.*",
"crossjoin/browscap": "~2.0",
"mixpanel/mixpanel-php" : "2.*",
"guzzlehttp/guzzle": "^6.2",
"geoip2/geoip2": "~2.0",
"overtrue/wechat": "~3.1",
"mongodb/mongodb": "^1.0.0",
"symfony/var-dumper": "^3.1",
"maxmind-db/reader": "~1.0",
"pda/pheanstalk": "^3.1",
"nesbot/carbon": "^1.21",
"sendgrid/sendgrid": "~5.1",
"ramsey/uuid": "^3.5",
"phpmyadmin/motranslator": "^3.0"
}
已尽力在 google 上搜索。有人可以指点我一份带有解释的文件吗?
我想通了。它是 PECL::Package::json_post
https://pecl.php.net/package/json_post
This extension provides a PHP content type handler for
"application/json" and "text/json" to PHP's form data parser. If the
Content-Type
of an incoming request is text/json
, the JSON
contents of the request body will by parsed into $_POST
.
我使用 remi repo 在 centos 上安装 PHP。它只是随我安装 PHP 5.6
PHP 中真的有一个未记录的功能来将 JSON POST 正文解码为 $_REQUEST 吗?
不等于PHP is automatically decoding JSON data?
我正在 PHP 中创建一个 API 以接收 FormData 和 JSON 中的参数。当我在 POSTMAN 中测试时,似乎 PHP 自动将带有 content-type:application/json
的 POST 主体解码为 $_REQUEST
。
我整个下午都在测试它,不知道怎么可能。
这是 post 请求:
curl -X POST \
http://xxxxx.com/api/user/profile/update \
-H 'content-type: application/json' \
-d '{
"id" : 174,
"email" : "reed@xxxxx.com",
"lang": "en_US",
}'
这是我在PHP
中得到的var_dump($_REQUEST)
//output
'id' => int 174
'email' => string 'reed@xxxxx.com' (length=19)
'lang' => string 'en_US' (length=5)
我看到的是错觉吗??
如果我将 POST 内容类型更改为任何非 application/json
的内容,则不会发生自动解码。
我正在使用 PHP5.6 + Phalcon 3.1。两者都没有提及自动解码 JSON.
这是我的 composer.json
,我认为这些软件包中的任何一个都不会做这样的事情。
"require": {
"twilio/sdk" : "4.*",
"crossjoin/browscap": "~2.0",
"mixpanel/mixpanel-php" : "2.*",
"guzzlehttp/guzzle": "^6.2",
"geoip2/geoip2": "~2.0",
"overtrue/wechat": "~3.1",
"mongodb/mongodb": "^1.0.0",
"symfony/var-dumper": "^3.1",
"maxmind-db/reader": "~1.0",
"pda/pheanstalk": "^3.1",
"nesbot/carbon": "^1.21",
"sendgrid/sendgrid": "~5.1",
"ramsey/uuid": "^3.5",
"phpmyadmin/motranslator": "^3.0"
}
已尽力在 google 上搜索。有人可以指点我一份带有解释的文件吗?
我想通了。它是 PECL::Package::json_post
https://pecl.php.net/package/json_post
This extension provides a PHP content type handler for "application/json" and "text/json" to PHP's form data parser. If the
Content-Type
of an incoming request istext/json
, the JSON contents of the request body will by parsed into$_POST
.
我使用 remi repo 在 centos 上安装 PHP。它只是随我安装 PHP 5.6