How to Use Guzzle in Laravel 10 Example?

Feb 27, 2023 . Admin



Hi friends,

In this tutorial, I am going to learn you laravel 10 HTTP guzzle request example. We will look at the example of laravel 10 HTTP guzzle request get,post and delete example. Here you will learn how to create HTTP guzzle request example in laravel 10. it's a simple example of laravel 10 guzzle HTTP client example.

If i talk about the primordial era of web development, then at that time we used to rely on cURL for a similar tasks. But as time went by, there were many improvements. Out of that development, Guzzle is one, i would talk about the Guzzle HTTP client today.

This tutorial will give you an example of HTTP guzzle request example in laravel 10. you can understand the concept of HTTP guzzle request in laravel 10.

Here i will example of HTTP guzzle request example in laravel 10. So let's see the bellow example:

1) Laravel 10 HTTP cURL GET Request Example

2) Laravel 10 HTTP cURL POST Request Example

3) Laravel 10 HTTP cURL PUT Request Example

4) Laravel 10 HTTP cURL DELETE Request Example

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2 : Laravel 10 HTTP cURL GET Request Example:

Here, we will see how to send curl http get request in laravel 10, let's update route file code and controller file code. you can see output as well:

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HttpPostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts', [HttpPostController::class, 'index']);
php artisan make:controller HttpPostController
app/Http/Controllers/HttpPostController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
  
class HttpPostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $response = Http::get('https://jsonplaceholder.typicode.com/posts');
    
        $jsonData = $response->json();
          
        dd($jsonData);
    }
}
Step 3 : Laravel 10 HTTP cURL POST Request Example: Here, we will see how to send curl http post request in laravel 10, let's update route file code and controller file code. you can see output as well: routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HttpPostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts/store', [HttpPostController::class, 'store']);
app/Http/Controllers/HttpPostController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
  
class HttpPostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store()
    {
        $response = Http::post('https://jsonplaceholder.typicode.com/posts', [
                    'title' => 'This is test from Nicesnippest.com',
                    'body' => 'This is test from Nicesnippest.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}
Output:
Array
(
    [titile] => This is test from Nicesnippest.com
    [body]  => This is test from Nicesnippest.com as body
    [id] => 101
)
Step 4 : Laravel 10 HTTP cURL PUT Request Example: Here, we will see how to send curl http put request in laravel 10, let's update route file code and controller file code. you can see output as well: routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HttpPostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts/update', [HttpPostController::class, 'update']);
app/Http/Controllers/HttpPostController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
  
class HttpPostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function update()
    {
        $response = Http::put('https://jsonplaceholder.typicode.com/posts/1', [
                    'title' => 'This is test from Nicesnippets.com',
                    'body' => 'This is test from Nicesnippets.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
Output:
Array
(
    [titile] => This is test from Nicesnippest.com
    [body]  => This is test from Nicesnippest.com as body
    [id] => 1
)
Step 5 : Laravel 10 HTTP cURL DELETE Request Example: Here, we will see how to send curl http delete request in laravel 10, let's update route file code and controller file code. you can see output as well: routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HttpPostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts/delete', [HttpPostController::class, 'delete']);
app/Http/Controllers/HttpPostController.php
<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
  
class HttpPostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function delete()
    {
        $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}
Step 6 : Laravel 10 API with Response: We will create very simple http request full example. we need to create simple route to call controller method. so let's create it: routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HttpPostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts', [HttpPostController::class, 'index']);
app/Http/Controllers/HttpPostController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
 
class HttpPostController extends Controller
{
    public function index()
    {
        $response = Http::get('http://jsonplaceholder.typicode.com/posts');
  
        $jsonData = $response->json();
          
        echo "<pre> status:";
        print_r($response->status());
        echo "<br/> ok:";
        print_r($response->ok());
        echo "<br/> successful:";
        print_r($response->successful());
        echo "<br/> serverError:";
        print_r($response->serverError());
        echo "<br/> clientError:";
        print_r($response->clientError());
        echo "<br/> headers:";
        print_r($response->headers());
    }
}
Output:
status:200
ok:1
successful:1
serverError:
clientError:
headers:Array
(
    [Date] => Array
        (
            [0] => Thu, 12 Mar 2020 06:08:58 GMT
        )
    [Content-Type] => Array
        (
            [0] => application/json; charset=utf-8
        )
    [Transfer-Encoding] => Array
        (
            [0] => chunked
        )
    .....
)

You can also get more information about Http Client in Laravel Docs: Click Here.

I hope it can help you...

#Laravel 10