php获取未解码之前的原始接口请求参数

前言

目前的几个项目,业务方基本都使用POST方式请求接口,本机磁盘会保留一份请求的原始参数用于请求分析和问题排查使用,一般有问题,也会基于seqid(请求唯一id)捞到日志,copy参数模拟请求看是否复现,但一直有个比较蛋疼的问题,PHP的$_POST$_GET$_REQUEST这些获取参数的方法获取到的数据都是经过编码后的,到时copy后可能无法直接使用,最近有同学吐槽,决定还是解决一下。

原文地址:https://www.ngxcode.com/archives/514.html

网上搜索很多解决方式,有通过file_get_contents,php input和自定义的一些方法,试了下都不大靠谱,要么根本不解决问题,要么太复杂不够灵活,突然想到了$_SERVERG$_GLOBALS打印了一下:

$_SERVER['REQUEST_URI']:请求地址,GET请求会同时带上url及地址内的参数
$_SERVER['QUERY_STRING']:请求参数,但只限于GET方式请求,POST无法获取到
$_GLOBALS['_POST']: POST请求参数
$_GLOBALS['_GET']: GET请求参数

以上获取到的参数都为未编码的原始参数信息。

$_SERVER

Array
(
    [HOSTNAME] => 10.93.50.82
    [SEARCH_ENV_IP] => 10.93.50.82
    [TERM] => xterm
    [HISTSIZE] => 1000
    [SEARCH_ENV_ENV] => online
    [USER] => daemon
    [LS_COLORS] => 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;'
    [SEARCH_ENV_GROUP] => aliyun
    [MAIL] => /var/spool/mail/root
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/php/bin
    [PWD] => /api
    [HISTCONTROL] => ignoredups
    [SHLVL] => 1
    [HOME] => /sbin
    [LOGNAME] => root
    [SEARCH_ENV_IDC] => aliyun
    [_] => /usr/local/php/sbin/php-fpm
    [OLDPWD] => /data1/minisearch/magent
    [HTTP_CONTENT_LENGTH] => 10140
    [HTTP_CONTENT_TYPE] => multipart/form-data; boundary=--------------------------309935001928920506739539
    [HTTP_CONNECTION] => keep-alive
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_HOST] => 10.93.80.88
    [HTTP_POSTMAN_TOKEN] => 7d97975a-bbd6-425c-8b8a-ad96a58c896e
    [HTTP_ACCEPT] => '*/*'
    [HTTP_USER_AGENT] => PostmanRuntime/7.29.2
    [REDIRECT_STATUS] => 200
    [SERVER_NAME] => 
    [SERVER_PORT] => 80
    [SERVER_ADDR] => 10.93.50.82
    [REMOTE_PORT] => 52515
    [REMOTE_ADDR] => 10.221.64.22
    [SERVER_SOFTWARE] => nginx/1.19.3
    [GATEWAY_INTERFACE] => CGI/1.1
    [REQUEST_SCHEME] => http
    [SERVER_PROTOCOL] => HTTP/1.1
    [DOCUMENT_ROOT] => /www/test
    [DOCUMENT_URI] => /apitest/mi.php
    [REQUEST_URI] => /apitest/mi.php?apitest=1
    [CONTENT_LENGTH] => 10140
    [CONTENT_TYPE] => multipart/form-data; boundary=--------------------------309935001928920506739539
    [REQUEST_METHOD] => POST
    [QUERY_STRING] => apitest=1
    [PATH_INFO] => 
    [SCRIPT_NAME] => /apitest/mi.php
    [SCRIPT_FILENAME] => /www/test/apitest/mi.php
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /apitest/mi.php
    [REQUEST_TIME_FLOAT] => 1683703077.2267
    [REQUEST_TIME] => 1683703077
)

$_GLOBALS

Array
(
    [_GET] => Array
        (
            [apitest] => 1
        )

    [_POST] => Array
        (
            [stream_entry_id] => 30
            [wb_version] => 6164
            [v_p] => 90
            [flowId] => 100103type%3D1%26q%3D%23%E6%9D%A8%E6%B4%8B%E7%8E%8B%E6%A5%9A%E7%84%B6%E6%81%8B%E6%83%85%23%26t%3D3
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )
    [_SERVER] => Array
        (
            [HOSTNAME] => 10.93.50.82
            [SEARCH_ENV_IP] => 10.93.50.82
            ...
        )

    [_REQUEST] => Array
        (
            [apitest] => 1
            [stream_entry_id] => 30
         	...
        )
)

结语

真是踏破铁鞋无觅处。。合理使用PHP服务器和全局变量,很多问题其实没有那么麻烦,拖这么久处理,还想着在nginx阶段做处理,也是个大大的无语。

posted @ 2023-05-09 20:06  wangnbx  阅读(80)  评论(0编辑  收藏  举报