【发布时间】:2015-04-01 23:47:20
【问题描述】:
我制作了一个简单的 PHP 应用程序,它提供 RESTful API 用于注册、登录和更新用户的地理位置。我在本地 Apache 服务器上使用 MySQL 数据库对其进行了测试,一切都很好。
最近我将PHP代码迁移到了一个在线主机HostGator。我的部分代码在涉及识别 HTTP 请求标头时停止工作。像这样:
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Authorization: f0cc696e44f76f9638331b5487f4b01d
Content-Type: application/x-www-form-urlencoded
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4
我编写了我的 PHP 代码,以便它读取标题 Authorization 下的 api-key。它在我的本地服务器上运行良好,但在 HostGator
上迁移相同的代码时报告 api-key missing我怀疑在我的本地环境中可能存在某些默认设置,但需要在 HostGator 中明确设置。这是我当前的 .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
AddType application/x-httpd-php54 .php
我将 XAMPP 用于本地服务器和数据库,并使用 Chrome 扩展程序 Advanced Rest Client 发送 HTTP 请求。
编辑:我检索标头值的代码:
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
$api_key = $headers['Authorization'];
// validating api key
// ...
} else {
// api key is missing in header
// ...
}
【问题讨论】:
标签: php apache .htaccess rest hosting