问题
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:
print_r($request->header());
or 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()
?
回答1:
An alternative to @Shedokan's answer using Laravel collections transform()
method instead of 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.
回答2:
Laravel uses Symphony, which keeps all header values in sub arrays, so we can just map the array and get them out:
$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
回答3:
You can user
$header = app('request')->header('header_name', 'default_header_value');
Can use this when $request
is not available
来源:https://stackoverflow.com/questions/42542061/how-to-get-all-the-headers-information-in-laravel-5-4