2017-03-01 138 views
2

所有的頭信息,當我打印,如:如何讓laravel 5.4

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()做所有的值?

+1

剛因爲你使用的laravel並不意味着你不能使用'getallheaders()' – nogad

+0

只能在apache上工作 – AndrewMcLagan

+0

是的,或者它是一個別名的函數,apache_request_headers()。起初,我認爲Laravel正在隱藏一些標題,但顯然不是;我將這些列表並排比較,並且一切都被考慮在內。 –

回答

0

Laravel使用交響樂,這使子陣列的所有頭值,因此我們就可以映射陣列,並讓他們出來:

$get_first = function($x){ 
    return $x[0]; 
}; 
// Same as getallheaders(), just with lowercase keys 
print_r(array_map($get_first, $request->headers->all())); 

注:所有按鍵均爲小寫

+0

是的,這只是使它更難以工作。 –