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"
}
*/