PHP奇怪的未定义索引错误

Cra*_*ite 10 php arrays http-headers

我有一个非常令人沮丧的问题,我无法检索任何标题.这是我的代码:

$headers = getallheaders();
echo($headers["SystemTime"]); //Doesnt work
$keys = array_keys($headers);
echo($headers[$keys[4]]); //Doesnt work
Run Code Online (Sandbox Code Playgroud)

两行都产生错误'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
)
Run Code Online (Sandbox Code Playgroud)

$ header的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"
}
Run Code Online (Sandbox Code Playgroud)

$ key的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"; 
} 
Run Code Online (Sandbox Code Playgroud)

回:

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: 
Run Code Online (Sandbox Code Playgroud)

我卡住了,我认真无法弄清楚出了什么问题.它应该工作.

PS.我知道SystemTime标头是非标准的.我从我的http_request提供.

Cra*_*ite 3

在你们的大力帮助下我得到了它。我有一种感觉,因为它在反序列化后起作用,可能编码不同,这就是为什么我们可以看到它,但不能触摸它?

我下面的代码获取所有标头并转换编码并将其放入新数组中:)

这是我的“翻译”代码。

    $headersRaw = getallheaders();
    $headers = array();
    foreach($headersRaw as $key => $value)
    {
        $headers[mb_convert_encoding($key, "UTF-8")] = $value;
    }
Run Code Online (Sandbox Code Playgroud)

  • 我认为你是 PH​​P Chuck Norris! (2认同)