Using Guzzle with Laravel Example

laravel 56 guzzle output

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. I am going to show step by step tutorial using Guzzle with Laravel. With guzzle, we will send http request to server and get http response. It is alternate to CURL which provide more features along with easy user interface. Different methods you can send with Guzzle are GET, DELETE, HEAD, PATCH, POST and PUT. Now, let’s get start integrating with laravel.

Install Guzzle

You can install Guzzle using composer

We can now use Guzzle Client class in our application.

Configure Route

You can use on your own. For now, I am defining a route and execute http request from controller. So, let’s define our route for sending a request to server.

Create Controller

Let’s create a new controller to test our Guzzle integration on our application. Simply run the following command inside of your project folder.

Modify Controller

Now, let’s open our newly created controller and make some changes. I have commented each line to help you understand the code. Our controller now looks like below:

laravel 56 guzzle output

Post Request using Guzzle

You can simply send post request like below:

Put Request using Guzzle

Delete Request using Guzzle

Async Requests

You can send asynchronous request using Guzzle.

Concurrent Request

You can also send Concurrent Request using promises and asynchronous requests.

You can find more examples and documentations in detail at this guzzle official documentation.