我遇到了一个非常令人沮丧的问题,无法检索任何标题。这是我的代码:
$headers = getallheaders();
echo($headers["SystemTime"]); //Doesnt work
$keys = array_keys($headers);
echo($headers[$keys[4]]); //Doesnt work
这两行都会产生错误,即“Undefined index: 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
)
$headers的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"
}
$keys变量的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:
我被卡住了,我真的想不出哪里出了错。它应该可以工作。
附注:我知道SystemTime头部是非标准的。我从我的http请求中提供它。
var_dump()
查看字符串(键)是否符合预期(例如,空字节或Unicode ws)。此外,“SystemTime”不是标准标题。谁添加了它?“任何标题”是否意味着您尝试了普通的标题? - mario$headers = getallheaders(); foreach ($headers as $name => $value) { echo "$name: $value. From Array: {$headers[$name]}\n"; }
的结果是什么。 - drew010Catchable fatal error: Object of class stdClass could not be converted to string
。 - Craig White