如何在 laravel 5.4 中获取所有的 headers 信息

新手上路,请多包涵

当我打印时:

 echo '<pre>';
print_r(getallheaders());

它给出输出

[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
)

但是当我使用 laravel 函数打印时,例如:

print_r($request->header());print_r($request->headers->all());

它从不在数组中打印出我需要的变量。它打印输出低于输出

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
        )

)

我如何打印 laravel 函数中的所有值,就像我可以用这个 getallheaders()

原文由 Muhammad 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 546
1 个回答

使用 Laravel 集合 transform() 方法而不是 array_map 方法替代@Shedokan 的答案:

 $headers = collect($request->header())->transform(function ($item) {
    return $item[0];
});

这样 $headers 将是一个集合,因此您可以使用 toArray() 方法转换回数组。由于这是一个集合,它还可以访问任何其他集合方法。

原文由 BlueC 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏