Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get all the headers information in laravel 5.4

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()?

like image 686
Muhammad Avatar asked Mar 01 '17 21:03

Muhammad


People also ask

How do I view headers in PHP?

The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request.

How to get request headers in Laravel?

In Laravel application, there are many ways you can get request headers. We will discuss about few ways from them. 1. Illuminate\Http\Request object Laravel provides many details in Illuminate\Http\Request class object. You can simply get headers details using headers () method. This will return all headers in array. 2.

How do I get a client's IP address in Laravel?

Request IP Address. The ip method may be used to retrieve the IP address of the client that made the request to your application: $ipAddress = $request->ip(); Content Negotiation. Laravel provides several methods for inspecting the incoming request's requested content types via the Accept header.

Is there an API centric setup with Laravel 4?

I have an API centric setup built with Laravel 4, consisting of a REST API, frontend and adminpanel. Both frontend and adminpanel are consuming the API using internal requests. I make internal requests like this: And everything works fine.

How to inspect the incoming request's requested content types in Laravel?

Laravel provides several methods for inspecting the incoming request's requested content types via the Accept header. First, the getAcceptableContentTypes method will return an array containing all of the content types accepted by the request:


1 Answers

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.

like image 136
BlueC Avatar answered Oct 11 '22 12:10

BlueC