# php获取请求的header头

**URL:** <https://www.shiqidu.com/t/topic/221>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2017 年9 月 6 日 03:26 UTC](https://www.shiqidu.com/t/topic/221 "2017-09-06T03:26:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年9 月 6 日 03:26 UTC](https://www.shiqidu.com/t/topic/221/1 "2017-09-06T03:26:38Z")

</div>

```php
function getRequestHeader($name = '')
{
    $header = [];
    if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
        $header = $result;
    } else {
        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'];
        }
    }
    $resp = array_change_key_case($header);
    if (is_array($name)) {
        return $resp = array_merge($resp, $name);
    }
    if ('' === $name) {
        return $resp;
    }
    $name = str_replace('_', '-', strtolower($name));
    return isset($resp[$name]) ? $resp[$name] : null;
}

```
