这是一个非常奇怪的。
我正在使用以下代码发出请求。
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Bearer: 5AE3lc//bq+k+M2M+tuGxW4K4K8=:i3kHKXStNXs9FnAg/IgWPNHYFBo=\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
$file = fopen($csvFile, 'r', false, $context);
在接收请求的脚本中,我有:
$headers = getallheaders();
var_dump($headers['Bearer']); // I get Undefined index: Bearer
$headers = json_decode(json_encode($headers), true);
var_dump($headers['Bearer']); //this one works.
但$ headers数组的var_dump在编码和解码之前或之后看起来相同。
//first var_dump
array(4) {
["Host"]=>
string(19) "" //removed
["Accept-Language"]=>
string(2) "en"
["Bearer"]=>
string(57) "5AE3lc//bq+k+M2M+tuGxW4K4K8=:i3kHKXStNXs9FnAg/IgWPNHYFBo="
["Cookie"]=>
string(7) "foo=bar"
}
//second var_dump, after decode/encode
array(4) {
["Host"]=>
string(19) "" //removed
["Accept-Language"]=>
string(2) "en"
["Bearer"]=>
string(57) "5AE3lc//bq+k+M2M+tuGxW4K4K8=:i3kHKXStNXs9FnAg/IgWPNHYFBo="
["Cookie"]=>
string(7) "foo=bar"
}
“Bearer”键是我从一开始就无法访问的唯一键。我可以毫无问题地得到其他人。
对于$ headers 的array_keys,var_dump
array(8) {
[0]=>
string(4) "Host"
[1]=>
string(10) "Connection"
[2]=>
string(10) "User-Agent"
[3]=>
string(6) "Bearer"
[4]=>
string(6) "Accept"
[5]=>
string(15) "Accept-Encoding"
[6]=>
string(15) "Accept-Language"
[7]=>
string(6) "Cookie"
}
为什么会这样?
答案 0 :(得分:0)
/
,=
和:
是HTTP标头中的分隔符。在定义$opts
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Bearer: " . urlencode('5AE3lc//bq+k+M2M+tuGxW4K4K8=:i3kHKXStNXs9FnAg/IgWPNHYFBo=') . "\r\n" .
"Cookie: foo=bar\r\n"
)
);