我有一個非常令人沮喪的問題,我無法檢索任何標題。這是我的代碼:PHP奇怪的未定義的索引錯誤
$headers = getallheaders();
echo($headers["SystemTime"]); //Doesnt work
$keys = array_keys($headers);
echo($headers[$keys[4]]); //Doesnt work
兩行都會產生錯誤'未定義索引:SystemTime'。
我不能爲我的生命弄清楚爲什麼我不能得到的價值。如果我去print_r($headers);
我得到這個
Array
(
[Content-Type] => application/x-www-form-urlencoded
[Content-Length] => 0
[Host] => localhost
[Referer] =>
[SystemTime] => 2012-06-26+09%3a20%3a27
)
的$的var_dump頭
array(5) {
["Content-Type"]=>
string(33) "application/x-www-form-urlencoded"
["Content-Length"]=>
string(1) "0"
["Host"]=>
string(9) "localhost"
["Referer"]=>
string(0) ""
["SystemTime"]=>
string(23) "2012-06-26+10%3a10%3a08"
}
的var_dump $鍵
array(5) {
[0]=>
string(12) "Content-Type"
[1]=>
string(14) "Content-Length"
[2]=>
string(4) "Host"
[3]=>
string(7) "Referer"
[4]=>
string(10) "SystemTime"
}
foreach ($headers as $name => $value) {
echo "$name: $value. From Array: {$headers[$name]}\n";
}
返回:
Connection: Keep-Alive. From Array: Keep-Alive
Content-Type: application/x-www-form-urlencoded. From Array: application/x-www-form-urlencoded
Content-Length: 0. From Array: 0
Host: localhost. From Array: localhost
Referer: . From Array:
Notice: Undefined index: SystemTime in /clientdata/apache-www/a/d/[XXXXXX].com/www/admin/request/GetPCLicence.php on line 22
SystemTime: 2012-06-26+10%3a10%3a08. From Array:
林卡住,我認真無法弄清楚是什麼出錯了。它應該工作。
PS。我知道SystemTime標題是非標準的。我從我的http_request中提供。
您使用的是什麼PHP版本? – drew010
使用'var_dump()'來查看字符串(鍵)是否是你期望的(例如空字節或unicode ws)。另外'SystemTime'不是標準標題。誰加了它?通過「任何標題」,你的意思是你嘗試了普通的? – mario
PHP版本5.2.17 –