php apache_request_headers函数 获取request HTTP请求头

1. apache_request_headers

简介

apache_request_headers,获取全部 HTTP 请求头信息,包含当前请求所有头信息的数组,失败返回 FALSE ; apache_request_headers PHP手册地址


/*
$_SERVER 中 HTTP请求头
{
	"HTTP_USER_AGENT": "PostmanRuntime\/7.26.8",
	"HTTP_ACCEPT": "*\/*",
	"HTTP_CACHE_CONTROL": "no-cache",
	"HTTP_POSTMAN_TOKEN": "a8016da5-fef1-486b-b432-17e5afec2bb6",
	"HTTP_HOST": "localhost:8099",
	"HTTP_CONNECTION": "keep-alive",
	"HTTP_CONTENT_TYPE": "application\/x-www-form-urlencoded",
	"HTTP_CONTENT_LENGTH": "28"
}
*/

/**
 * thinkphp6 Request类代码
 */ 
if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
    $header = $result;
} else {
    // 没有 apache_request_headers函数替代方法
    $header = [];
    $server = $_SERVER;
    foreach ($server as $key => $val) {
        if (0 === strpos($key, 'HTTP_')) {
            $key          = str_replace('_', '-', strtolower(substr($key, 5)));
            $header[$key] = $val;
        }
    }
    if (isset($server['CONTENT_TYPE'])) {
        $header['content-type'] = $server['CONTENT_TYPE'];
    }
    if (isset($server['CONTENT_LENGTH'])) {
        $header['content-length'] = $server['CONTENT_LENGTH'];
    }
}
/*
{
	"User-Agent": "PostmanRuntime\/7.26.8",
	"Accept": "*\/*",
	"Cache-Control": "no-cache",
	"Postman-Token": "79b20a0d-674f-4d1d-b84d-a582510802b6",
	"Host": "localhost:8099",
	"Connection": "keep-alive",
	"Content-Type": "application\/x-www-form-urlencoded",
	"Content-Length": "28"
}
*/

由于HTTP请求头,单词首字母大写为了方便使用,可以用到array_change_key_case;

2. array_change_key_case

简介

array_change_key_case — 将数组中的所有键名修改为全大写或小写,如果输入值(array)不是一个数组,那么返回FALSE;array_change_key_case PHP手册地址

/*
{
	"User-Agent": "PostmanRuntime\/7.26.8",
	"Accept": "*\/*",
	"Cache-Control": "no-cache",
	"Postman-Token": "79b20a0d-674f-4d1d-b84d-a582510802b6",
	"Host": "localhost:8099",
	"Connection": "keep-alive",
	"Content-Type": "application\/x-www-form-urlencoded",
	"Content-Length": "28"
}
*/

$header = array_change_key_case($header);
/*
{
	"user-agent": "PostmanRuntime\/7.26.8",
	"accept": "*\/*",
	"cache-control": "no-cache",
	"postman-token": "0da50ae6-b6cb-46f1-b69e-834d5aa576f3",
	"host": "localhost:8099",
	"connection": "keep-alive",
	"content-type": "application\/x-www-form-urlencoded",
	"content-length": "28"
}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值