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

How to Get Current URL in Laravel


how to get current url in laravel, laravel get current url, get current page url in laravel, get full url in laravel, find current url in laravel,

Laravel 9 Install React Auth Tutorial with Example


In this article, I'll demonstrate how to use the Laravel UI and React Auth scaffolding to create login, register, logout, forget password, profile, an...

Laravel Routing Tutorial Step by Step


I'll give you a step-by-step instruction on laravel routing in this blog. We'll demonstrate how to make a new route in Laravel and how to make a route...

Laravel 8/7/6 Google ReCaptcha v2 Form Validation


In form validation, Laravel 8,7,6 uses Google v2 Captcha/Re-Captcha. You will learn how to add Google v2 ReCaptcha form validation to a Laravel form i...

Laravel whereMonth and whereYear Example


laravel whereMonth and whereYear example, whereMonth, whereYear, where condition in laravel 8, date function in laravel 8, whereMonth and whereYear in...