如何在PHP中读取自定义HTTP标头

问题描述 投票:0回答:1

我已经在我的JQuery ajax请求中设置了自定义HTTP标头,如下所示:

url:BASEURL+"path/to/my_api/endpoint.php",
headers:{
    'Authorization':'Basic',
    'App-Version':'1.3',
    'Key':'123456',
    'secret':'090909'
}

我希望能够阅读目标php脚本中的内容。我试过了:

print_r(getallheaders());

但是打印的数组是这个:

Array
(
    [Host] => localhost
    [Connection] => keep-alive
    [Content-Length] => 153
    [Accept] => application/json, text/javascript, */*; q=0.01
    [Origin] => http://localhost
    [X-Requested-With] => XMLHttpRequest
    [User-Agent] => Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+
    [Content-Type] => application/x-www-form-urlencoded
    [Sec-Fetch-Site] => same-origin
    [Sec-Fetch-Mode] => cors
    [Referer] => http://localhost/maxiko/application/test/index.html
    [Accept-Encoding] => gzip, deflate, br
    [Accept-Language] => en-US,en;q=0.9
    [Cookie] => _ga=GA1.1.1030525951.1577774346; PHPSESSID=tm0ltrbt8op7bkksvh7mj9rdvs
)

它不包含我的自定义键值对。那么我该如何实现呢?

php jquery ajax http-headers
1个回答
0
投票

我尝试在自己的项目中添加print_r(getallheaders());,并使用邮递员发送了secret标头。打印得很好。如果您完全确定将发送标头,那么我的猜测是您正在以cli(来自apache或nginx)而不是apache模块的形式运行php。尝试查找$_SERVER['HTTP_SECRET'],它可能已经包含标题。但是,很难在不知道正在运行什么(apache,nginx)或查看配置的情况下提供解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.