What role may Guzzle HTTP Client play in your PHP Laravel GET and POST requests? We will attempt to respond to that in this tutorial. If you've been searching the internet for a Laravel Guzzle http client example, your search is over.

If I think back to the early days of web development, we relied on cURL to perform tasks like this. However, a lot of advancements were made over time. Guzzle is one of such developments, and now I'd like to talk about the Guzzle HTTP client.

We'll look at how to build the Guzzle HTTP Client in Laravel and learn how to submit HTTP requests in this tutorial.

Install Guzzle Http Package

Guzzle is a PHP HTTP client that makes it simple to interface with online services and submit HTTP queries. It offers a user-friendly interface that is both straightforward and effective for making POST requests, streaming huge uploads and downloads, using HTTP cookies, uploading JSON data, etc.

Ideally, we need to install the guzzlehttp/guzzle package using Composer package manager in order to send HTTP queries.

composer require guzzlehttp/guzzle

Guzzle 6's best feature, and what draws my attention to it, is the ability to simultaneously submit synchronous and asynchronous queries from the same interface. Additionally, this provides countless settings for use with http requests.

Guzzle HTTP Client CRUD Requests

You may get a general concept about making GET, POST, PUT, and DELETE requests with Guzzle 6 HTTP Client library in Laravel from the information provided below.

Guzzle GET Request

public function guzzleGet()
{
    $client = new \GuzzleHttp\Client();
    $request = $client->get('http://testmyapi.com');
    $response = $request->getBody();
   
    dd($response);
}

Guzzle POST Request

public function guzzlePost()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://testmyapi.com/api/blog";
    $myBody['name'] = "Demo";
    $request = $client->post($url,  ['body'=>$myBody]);
    $response = $request->send();
    
    dd($response);
}

Guzzle PUT Request

public function guzzlePut()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://testmyapi.com/api/blog/1";
    $myBody['name'] = "Demo";
    $request = $client->put($url,  ['body'=>$myBody]);
    $response = $request->send();
   
    dd($response);
}

Guzzle DELETE Request

public function guzzleDelete()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://testmyapi.com/api/blog/1";
    $request = $client->delete($url);
    $response = $request->send();
    dd($response);
}


Recommended Posts

View All

Laravel 9 Highcharts Example Tutorial


In this tutorial, I'll give you an example of how to use highchart js to construct a highchart in a laravel 9 application.

Laravel 9 Create Multi Language Website Example Tutorial


Learn how to create a multi-language website with Laravel 9 through our example tutorial. Develop your skills and broaden your audience reach today!

Laravel Multiple Where Condition Example


Laravel multiple where condition, Laravel multiple where not working, Laravel multiple where same column, Laravel 8 Eloquent multiple where condition...

Google Map with Multiple Marker and Info Box in Laravel


This tutorial will teach you how to use Laravel to integrate a Google Map with numerous markers and an info box.

Laravel 9 Ajax Form Validation Example


In Laravel 9, we may ajax submit a form following validation. Laravel's ajax form validation is simple.