PHP Header 存在但无法检索

PHP Header exists but cannot retrieve

我正在使用此方法使 apache header 请求在 nginx 中工作。

if (!function_exists('apache_request_headers')) { 
    function apache_request_headers() { 
        foreach($_SERVER as $key=>$value) { 
            if (substr($key,0,5)=="HTTP_") { 
                $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5))))); 
                $out[$key]=$value; 
            }else{ 
                $out[$key]=$value; 
            } 
        } 
        return $out; 
    } 
} 

我像这样检索 header $headers = apache_request_headers(); 并使用数组来保存 json。

$response=array()
$response["error"] = true;
$response["message"] = $headers;

下面的代码是 $response 数组变量中的内容:

{
error: true
message: {
CONTENT_LENGTH: "13"
CONTENT_TYPE: "application/x-www-form-urlencoded"
DOCUMENT_ROOT: "/home4/admin/public_html"
GATEWAY_INTERFACE: "CGI/1.1"
Accept: "*/*"
Accept-Encoding: "gzip, deflate"
Accept-Language: "en-US,en;q=0.8"
Connection: "close"
Cookie: "_ga=GA1.2.1266385826.1428275832"
Host: "mysite.com"
Origin: "chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo"
User-Agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"
X-Http-Proto: "HTTP/1.1"
X-Log-7528: "107.147.160.193"
X-Real-Ip: "107.147.160.193"
PATH: "/bin:/usr/bin"
PHPRC: "/home4/admin"
QUERY_STRING: ""
REDIRECT_STATUS: "200"
REDIRECT_UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
REDIRECT_URL: "/rmapp/v1/tasks"
REMOTE_ADDR: "107.147.160.193"
REMOTE_PORT: "31527"
REQUEST_METHOD: "POST"
REQUEST_URI: "/rmapp/v1/tasks"
SCRIPT_FILENAME: "/home4/admin/public_html/rmapp/v1/index.php"
SCRIPT_NAME: "/rmapp/v1/index.php"
SERVER_ADDR: "192.185.226.161"
SERVER_ADMIN: "webmaster@mysite.com"
SERVER_NAME: "mysite.com"
SERVER_PORT: "80"
SERVER_PROTOCOL: "HTTP/1.1"
SERVER_SIGNATURE: "<address>Apache Server at mysite.com Port 80</address> "
SERVER_SOFTWARE: "Apache"
UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
PHP_SELF: "/rmapp/v1/index.php"
REQUEST_TIME: 1431921894
argv: [0]
argc: 0
}-
}

我的问题是,我需要从 $header 中获取 X-ApiKey,但没有使用 $api_key = $headers['X-ApiKey']; returns,但是如您所见,X-ApiKey 存在于 $header。有人可以告诉我我在这里缺少什么吗?

$response["message"] 是一个 string。首先尝试explode它然后访问它-

$temp = explode('X-Apikey:', $response['message']);
$temp1 = explode('X-Http-Proto:', $temp[1]);
var_dump(trim(str_replace('"', '', $temp1[0])));

输出

string(32) "bca0de3e7c10cb6623ef00021caf9450"

您似乎使用了错误的变量名。变量名称区分大小写。

响应returns如下(Apikey中的小k): X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"

当您使用大写字母 k $api_key = $headers['X-ApiKey'];

尝试以下操作: $api_key = $headers['X-Apikey'];