ItSolutionStuff.com

Laravel 11 Guzzle HTTP Request Example

By Hardik Savani • November 18, 2024
Laravel

In this post, I will show how to send Guzzle HTTP client requests in a Laravel 11 application.

Laravel 11 provides a built-in HTTP Client using the GuzzleHttp/Guzzle package. You can easily run HTTP client requests using the HTTP facade. You can send GET, POST, PUT, and DELETE requests, and easily get responses with text and JSON as well. Additionally, you can easily pass headers and authentication tokens.

Step for how to call HTTP API request from Laravel 11?

  • 1) Laravel 11 HTTP cURL GET Request Example
  • 2) Laravel 11 HTTP cURL POST Request Example
  • 3) Laravel 11 HTTP cURL PUT Request Example
  • 4) Laravel 11 HTTP cURL DELETE Request Example
  • 5) Laravel 11 API with Response

Let's see one by one example.

laravel 11 http guzzle request

Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

1) Laravel 11 HTTP cURL GET Request Example:

Here, we will see how to send a cURL HTTP GET request in Laravel 11. Let's update the route file code and controller file code. You can see the output as well.

routes/web.php

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

Output:

2) Laravel 11 HTTP cURL POST Request Example:

Here, we will see how to send a cURL HTTP POST request in Laravel 11. Let's update the route file code and controller file code. You can see the output as well:

routes/web.php

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

Output:

3) Laravel 11 HTTP cURL PUT Request Example:

Here, we will see how to send a cURL HTTP PUT request in Laravel 8. Let's update the route file code and controller file code. You can see the output as well.

routes/web.php

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

Output:

4) Laravel 11 HTTP cURL DELETE Request Example:

Here, we will see how to send a cURL HTTP DELETE request in Laravel 11. Let's update the route file code and controller file code. You can see the output as well.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PostController;
    
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) Laravel 11 API with Response:

We will create a very simple HTTP request full example. We need to create a simple route to call a controller method. So let's create it.

routes/web.php

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

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.

Output:

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 11 Yajra Datatables Example Tutorial

Read Now →

Laravel 11 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel 11 Ajax Request Example Tutorial

Read Now →

Laravel 11 Markdown | Laravel 11 Send Email using Markdown Mailables

Read Now →

Laravel 11 Ajax Form Validation Example Tutorial

Read Now →

How to Create, Run & Rollback Migration in Laravel 11?

Read Now →

Laravel 11 Ajax Image Upload Example

Read Now →

Laravel 11 Authentication using Jetstream Tutorial

Read Now →

Laravel 11 Authentication - Install Laravel 11 Breeze Tutorial

Read Now →

Laravel 11 Create Custom Helper Functions Example

Read Now →

Laravel 11 Import Export Excel and CSV File Tutorial

Read Now →

Laravel 11 Generate PDF File using DomPDF Example

Read Now →

Laravel 11 CRUD Application Example Tutorial

Read Now →

How to use new Dumpable Trait in Laravel 11?

Read Now →