【发布时间】:2012-06-26 00:02:31
【问题描述】:
我有一个非常令人沮丧的问题,我无法检索任何标题。这是我的代码:
$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
)
$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_request 中提供。
【问题讨论】:
-
你使用的是什么 PHP 版本?
-
使用
var_dump()查看字符串(键)是否符合您的预期(例如空字节或unicode ws)。SystemTime也不是标准标题。谁加的? “任何标题”是指您尝试了普通标题吗? -
我的客户端是 C#。我知道它 100% 有效。我上面贴的代码实际上是我代码的前几行。
-
我很想知道
$headers = getallheaders(); foreach ($headers as $name => $value) { echo "$name: $value. From Array: {$headers[$name]}\n"; }的结果。 -
您的 foreach 结果已发布在上方
标签: php arrays http-headers