Laravel 10 Guzzle Http Request Example

Guzzle's simplicity and flexibility make it ideal for testing HTTP requests in Laravel. You can use it to simulate HTTP requests during testing, allowing you to mock external API responses and ensure the correctness of your application's interactions with external services.

Guzzle provides convenient methods for handling response data. You can easily retrieve the response body, headers, status codes, and other relevant information. Additionally, Guzzle includes built-in support for parsing popular data formats like JSON and XML.

blockquote>

Step 1: Install Laravel App

composer create-project laravel/laravel example-Guzzle

1)  HTTP cURL GET Request Example:

routes/web.php 

<?php
  
    use Illuminate\Support\Facades\Route;
      
    use App\Http\Controllers\PostController;
      
    /*
    |--------------------------------------------------------------------------
    | 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', [PostController::class, 'index']);

app/Http/Controllers/PostController.php

<?php
  
    namespace App\Http\Controllers;
      
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Http;
      
    class PostController extends Controller
    {
        /**
         * Write code on Method
         *
         * @return response()
         */
        public function index()
        {
            $response = Http::get('https://jsonplaceholder.typicode.com/posts');
        
            $jsonData = $response->json();
              
            dd($jsonData);
        }
    }

2) HTTP cURL POST Request Example:

routes/web.php 

<?php
  
    use Illuminate\Support\Facades\Route;
      
    use App\Http\Controllers\PostController;
      
    /*
    |--------------------------------------------------------------------------
    | 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', [PostController::class, 'store']);

app/Http/Controllers/PostController.php

<?php
  
    namespace App\Http\Controllers;
      
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Http;
      
    class PostController extends Controller
    {
        /**
         * Write code on Method
         *
         * @return response()
         */
        public function store()
        {
            $response = Http::post('https://jsonplaceholder.typicode.com/posts', [
                        'title' => 'This is test from ItSolutionStuff.com',
                        'body' => 'This is test from ItSolutionStuff.com as body',
                    ]);
      
            $jsonData = $response->json();
          
            dd($jsonData);
        }
    }

3)HTTP cURL PUT Request Example:

routes/web.php 

<?php
  
  use Illuminate\Support\Facades\Route;
    
  use App\Http\Controllers\PostController;
    
  /*
  |--------------------------------------------------------------------------
  | 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', [PostController::class, 'update']);

app/Http/Controllers/PostController.php

<?php
  
  namespace App\Http\Controllers;
    
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\Http;
    
  class PostController 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 ItSolutionStuff.com',
                      'body' => 'This is test from ItSolutionStuff.com as body',
                  ]);
    
          $jsonData = $response->json();
        
          dd($jsonData);
      }
  }

4)  HTTP cURL DELETE Request Example:

routes/web.php

<?php
  
  use Illuminate\Support\Facades\Route;
    
  use App\Http\Controllers\PostController;
    
  /*
  |--------------------------------------------------------------------------
  | 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', [PostController::class, 'delete']);

app/Http/Controllers/PostController.php

<?php
   
   namespace App\Http\Controllers;
     
   use Illuminate\Http\Request;
   use Illuminate\Support\Facades\Http;
     
   class PostController 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);
       }
   }

5)  API with Response:

routes/web.php 

<?php
  
  use Illuminate\Support\Facades\Route;
    
  use App\Http\Controllers\PostController;
    
  /*
  |--------------------------------------------------------------------------
  | 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', [PostController::class, 'index']);

app/Http/Controllers/PostController.php

<?php
  
  namespace App\Http\Controllers;
    
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\Http;
   
  class PostController 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());
      }
  }

Check Output.