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

2023/5/10 1:22:21

本文主要是介绍php获取未解码之前的原始接口请求参数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前言

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

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

[REQUEST_URI][QUERY_STRING]这俩不就是请求的原始url和参数吗,😂,真是踏破铁鞋无觅处。。。

Array
(
    [HOSTNAME] => 218c76b4dd62
    [SEARCH_ENV_IP] => 127.0.0.1
    [TERM] => xterm
    [HISTSIZE] => 1000
    [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;
    [SEARCH_ENV_GROUP] => inner
    [MAIL] => /var/spool/mail/root
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/php/bin
    [PWD] => /data1/minisearch/discovery
    [app_status] => dev
    [LANG] => en_US.UTF-8
    [HISTCONTROL] => ignoredups
    [SHLVL] => 1
    [HOME] => /sbin
    [LOGNAME] => root
    [SEARCH_ENV_IDC] => inner
    [_] => /usr/local/php/sbin/php-fpm
    [OLDPWD] => /data1/minisearch/magent
    [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_ACCEPT] => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
    [HTTP_USER_AGENT] => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_CONNECTION] => keep-alive
    [HTTP_HOST] => 127.0.0.1:8088
    [REDIRECT_STATUS] => 200
    [SERVER_NAME] => 
    [SERVER_PORT] => 80
    [SERVER_ADDR] => 172.17.0.19
    [REMOTE_PORT] => 62909
    [REMOTE_ADDR] => 10.222.64.22
    [SERVER_SOFTWARE] => nginx/1.15.8
    [GATEWAY_INTERFACE] => CGI/1.1
    [REQUEST_SCHEME] => http
    [SERVER_PROTOCOL] => HTTP/1.1
    [DOCUMENT_ROOT] => /data1/openresty/htdocs
    [DOCUMENT_URI] => /api/test.php
    [REQUEST_URI] => /api/test.php?stream_entry_id=0&wb_version=6164&bigday_max_enable=1&containerid=100103type%3D1%26q%3D%23%E9%B4%8A%E8%B4%A4%E6%B5%8B%E8%AF%95%E6%91%84%E6%94%BF%E8%AF%9D%E9%A2%9802%23%26t%3D4&&ignore_inturrpted_error=true
    [CONTENT_LENGTH] => 
    [CONTENT_TYPE] => 
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => stream_entry_id=0&wb_version=6164&bigday_max_enable=1&containerid=100103type%3D1%26q%3D%23%E9%B4%8A%E8%B4%A4%E6%B5%8B%E8%AF%95%E6%91%84%E6%94%BF%E8%AF%9D%E9%A2%9802%23%26t%3D4&&ignore_inturrpted_error=true
    [PATH_INFO] => 
    [SCRIPT_NAME] => /api/test.php
    [SCRIPT_FILENAME] => /www/api/test.php
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /api/test.php
    [REQUEST_TIME_FLOAT] => 1683629478.9321
    [REQUEST_TIME] => 1683629478
)

结语

这么简单的问题,拖这么久也是服了自己,还想着在nginx阶段做处理,也是个大大的无语。



这篇关于php获取未解码之前的原始接口请求参数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程