Guzzle HTTP Request Example using LARAVEL 10

Spread the love

Hello Guys,

Let’s explore a straightforward example of a Guzzle HTTP Request within the context of Laravel 10 in this article. We’ll delve into an illustrative HTTP client request using the Laravel 10 framework. For those looking to learn more about this Laravel 10 Guzzle HTTP client example, we’ll explain PHP Laravel 10 HTTP client requests step by step.

In Laravel 10, a built-in HTTP client is readily accessible, thanks to the guzzlehttp/guzzle package. The HTTP façade simplifies the execution of HTTP client requests. Whether you’re sending GET, POST, PUT, or DELETE queries, you can easily retrieve text and JSON responses. Additionally, handling headers and authentication tokens is a straightforward process.

Here, I will demonstrate how to make HTTP API requests using Laravel 10 with basic examples.

I will provide you with a series of Guzzle HTTP Request examples one at a time.

Install Laravel 10

While it’s not mandatory, you can go ahead and execute the following command if you haven’t already established your Laravel application:

composer create-project laravel/laravel example-app

1) Laravel 10 HTTP cURL GET Request Example:

In this section, we will explore the process of code editing within the Laravel 10 route and controller files. Additionally, we will highlight the visibility of the output.

Also Read: How to Create Custom Helper Functions in LARAVEL 10

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) Laravel 10 HTTP cURL POST Request Example:

In this guide, we’ll explore the process of modifying controller and route files within Laravel 10, while also demonstrating how to initiate a CURL HTTP POST request. We’ll ensure that the resulting output is clearly visible throughout the tutorial.

Also Read: How to Add Foreign Key in MIGRATION using Laravel?

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 itcodestuff.com',
                    'body' => 'This is test from itcodestuff.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}

3) Laravel 10 HTTP cURL PUT Request Example:

In this article, we will explore the process of updating controller and route files in Laravel 10 using a CURL HTTP PUT request. Additionally, we will discuss how to view the output of this operation.

Also Read: How to Rollback Migration in Laravel?

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 itcodestuff.com',
                    'body' => 'This is test from itcodestuff.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}

4) Laravel 10 HTTP cURL DELETE Request Example:

In this guide, we will explore the process of modifying controller and route files in Laravel 10 while demonstrating how to send a CURL HTTP delete request. Additionally, we will showcase the visibility of the output.

Also Read: How to Use LARAVEL DB Raw Query?

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) Laravel 10 API with Response:

Explore a Simple HTTP Request Example: Creating a Direct Route to Access the Controller Method

Also Read: How To Send Email In Laravel 9

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());
    }
}

Your quest for the best Laravel blog ends here, where expert knowledge meets curated excellence.

Leave a Comment