Using Guzzle to Retrieve URL Header Data in Laravel 5.3

By Tyler Jefford • November 28th, 2016

Everyday I'm Guzzling cover image

I have recently been coding an application in Laravel that was using a basic cURL request to grab some information about a url and display that to the users. While this approach worked, I wanted to make it cleaner and more maintainable, so to do this I installed Guzzle. This is a super easy composer command, but if you are still having trouble, there is an awesome tutorial here on medium.

Once I had Guzzle up and running making my first call was super simple. But I just wasn't getting the deep data that I was getting from the cURL request like port number, transfer time and redirects.

Output of cURL respone in Guzzle

Response from below code

After digging through the documentation and many different stackoverflow discussions I came across using on_stats in my request. This allows me to dump all the header data that I wanted. But it cant just be called through the request like you can with statusCode. So I had to come up with a clever way to retain the data so I can use it later.

Since this is a standard Laravel Model, it is a traditional OOP class. So I created a protected array at the class level that I will push into from a custom method that will take input and merge into that array.

Here is the code I used to collect and reuse domain header data from a Guzzle request in Laravel.