PHP接收http请求头信息

1、PHP 自带函数 getallheaders()

  • 目前 getallheaders() 只能用于 apache 中。如果想在 nginx 中也能使用,可以使用自定义函数。
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

 

2、自定义函数

function em_getallheaders()
{
    $headers = [];
    foreach ($_SERVER as $name => $value) {
        if (substr($name, 0, 5) == 'HTTP_') {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    return $headers;
}

posted @ 2018-11-10 22:09  幽篁晓筑  阅读(4276)  评论(0编辑  收藏  举报