php 如何在laravel 5.4中获取所有标题信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42542061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get all the headers information in laravel 5.4
提问by Muhammad
when i print like:
当我打印时:
echo '<pre>';
print_r(getallheaders());
it gives output
它给出输出
[Host] => abc.com
[User-Agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[Accept-Language] => en-US,en;q=0.5
[Accept-Encoding] => gzip, deflate
[Cookie] => someth
[Upgrade-Insecure-Requests] => 1
[Cache-Control] => max-age=0
[SM_TRANSACTIONID] => 000000000000000000000000b7857360-1499-58b735e6-68944700-eed22930b94f
[SM_SDOMAIN] => abc.com
[SM_REALM] => REALM-BEACONTRACK-DEV-Protect root
[SM_REALMOID] => 06-000cd148-15d2-18a7-a771-71810afc4027
[SM_AUTHTYPE] => Form
....... many more
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
[Referer] => http://127.0.0.1:8000
[Accept-Encoding] => gzip, deflate, sdch, br
[Accept-Language] => en-US,en;q=0.8
[Cookie] => XSRF-TOKEN=eyJpdiI6Im1UZ012bXhRQ1VVdEUra1d3Yko4ZEE9PSIsInZhbHVlIjoidnFqN0l6VUVBVjdKd2hWUitHazhlTWN1Z2puSW1LMlZTTU4yYW1GcVwvQWg1aEpkNklZWUkranBIZ3R1UGoxUUdMU1VFWVEzekViWTluSkk1c0FjNlZ3PT0iLCJtYWMiOiIxNGFmMGE1OWQ3OWNlZWY1Y2E4OGQ4MzY1MDg3ZmM2MDY5NzVmYTI2YmE3MzA3YWU2M2U2YjkyOWEzZTMzYWFkIn0%3D; beaconTrack_session=eyJpdiI6ImVWazMyK2JLbXlrN0lxMEVEdE1pTlE9PSIsInZhbHVlIjoiSTdIbVkyWmROSDZBXC8xVmZJdHEycmgwOFpFUm1BNUtWVFNyQjF0MjY5TTV6Qkd1aUFGSEJBcmRrQ3hvM1BxVXdld0tjWlwvcVNEeXcwQmdjWW5yUFwvb1E9PSIsIm1hYyI6IjgzZjRiOGExODc2NmI3Y2JjNDY1MWViMThlZmE0ODlhYjMyYzllMTE1OTNhNjM1NWE1ZDc0NWViZDFkMjA3ZTIifQ%3D%3D
)
but when i print using laravel functions like:
但是当我使用 laravel 函数打印时,例如:
print_r($request->header());
or print_r($request->headers->all());
print_r($request->header());
或者 print_r($request->headers->all());
it never print out my required variables in array. it print outs below output
它永远不会在数组中打印出我需要的变量。它在输出下方打印出来
Array
(
[host] => Array
(
[0] => abc.com
)
[connection] => Array
(
[0] => keep-alive
)
[cache-control] => Array
(
[0] => max-age=0
)
[upgrade-insecure-requests] => Array
(
[0] => 1
)
[user-agent] => Array
(
[0] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
)
[accept] => Array
(
[0] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
)
[accept-encoding] => Array
(
[0] => gzip, deflate, sdch
)
[accept-language] => Array
(
[0] => en-US,en;q=0.8
)
[cookie] => Array
(
[0] => cookie
)
)
how i can print all the values in laravel function the same i can do with this
getallheaders()
?
我如何打印 laravel 函数中的所有值,就像我可以用这个一样
getallheaders()
?
回答by Shedokan
Laravel uses Symphony, which keeps all header values in sub arrays, so we can just map the array and get them out:
Laravel 使用 Symphony,它将所有标头值保存在子数组中,因此我们可以映射数组并将它们取出:
$get_first = function($x){
return $x[0];
};
// Same as getallheaders(), just with lowercase keys
print_r(array_map($get_first, $request->headers->all()));
Note:All keys are lowercase
注意:所有键都是小写的
回答by BlueC
An alternative to @Shedokan's answer using Laravel collectionstransform()
method instead of array_map
:
使用 Laravel集合transform()
方法代替@Shedokan 的答案的替代方法array_map
:
$headers = collect($request->header())->transform(function ($item) {
return $item[0];
});
This way $headers
will be a collection so you can then use the toArray()
method to convert back to an array. Since this is a collection it also has access to any of the other collection methods.
这种方式$headers
将是一个集合,因此您可以使用该toArray()
方法将其转换回数组。由于这是一个集合,它也可以访问任何其他集合方法。
回答by WILLIAM DAZA
You can user
您可以使用
$header = app('request')->header('header_name', 'default_header_value');
Can use this when $request
is not available
$request
不可用时可以使用它