如何从webhook请求者获取HTTPS标头

时间:2018-07-24 20:54:07

标签: request-headers

我有一个Webhook来接收我要配置的更新。我需要从标题中获取承载令牌,但无法检索它。有人可以阐明这个问题吗?我很沮丧!

接收网址为https://example.com/receive

$data = file_get_contents("php://input",true);
$events= json_decode($data, true);

1 个答案:

答案 0 :(得分:0)

如果您要查找OAuth承载令牌,通常会在请求HTTP授权标头中传输这些令牌。在PHP中,由于不同的Web服务器具有不同的读取Authorization标头的方法,因此读取它们有些棘手。

有一个很好的示例,说明如何在this answer中读取承载令牌。为方便起见,在此处复制:

<?PHP
/** 
 * Get hearder Authorization
 * */
function getAuthorizationHeader() {
    $headers = null;
    if (isset($_SERVER['Authorization'])) {
        $headers = trim($_SERVER["Authorization"]);
    } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
        $headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
    } elseif (function_exists('apache_request_headers')) {
        $requestHeaders = apache_request_headers();
        // Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
        $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
        if (isset($requestHeaders['Authorization'])) {
            $headers = trim($requestHeaders['Authorization']);
        }
    }
    return $headers;
}
/**
 * get access token from header
 * */
function getBearerToken() {
    $headers = getAuthorizationHeader();
    // HEADER: Get the access token from the header
    if (!empty($headers)) {
        if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
            return $matches[1];
        }
    }
    return null;
}
?>