How To Work Laravel 9 Guzzle Http Request ?

Today now in this post i will show you How To Work Laravel 9 Http Request ? So here, I will teach you how we can work laravel 9 http request example. if you want to know about an example of laravel 9 http client request example then i hope you are in the right place. Here i will explain all step by step laravel 9 guzzle http client example. So in this tutorial i will give you a very simple example of php laravel 9 http client request. We will do this by the following things for laravel 9 Guzzle http Client post request.

We know that Laravel 9 provides an inbuilt HTTP Client by using the guzzlehttp/guzzle package. We can easily run the HTTP client requests by using the HTTP facade. We can send all GET, POST, PUT, DELETE requests with we can easily get the responses with text and JSON too. we can also pass the header and authentication tokens easily.

Here, I will give you a very simple examples of how we can run a call HTTP API request from the laravel 9?

  1. Laravel 9 HTTP cURL GET Request Example
  2. Laravel 9 HTTP cURL POST Request Example
  3. Laravel 9 HTTP cURL PUT Request Example
  4. Laravel 9 HTTP cURL DELETE Request Example
  5. Laravel 9 API with the Response

So Let’s see all one by one example.

Install Laravel 9:

This is the first step. This step is not need if you already install laravel 9. however, if you don’t have then need to created the laravel application, then we may go ahead and need to execute the below command:

composer create-project laravel/laravel example-app
1) Laravel 9 HTTP cURL GET Request Example:

Here, i will show you how to send curl http with the get request in laravel 9, let’s need to update route file code and controller file code.

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 9 HTTP cURL POST Request Example:

Here, i will show you how to send curl http with the post request in laravel 9, let’s need to update route file code and controller file code.

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 codingspoint.com',
                    'body' => 'This is test from codingspoint.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}
3) Laravel 9 HTTP cURL PUT Request Example:

Here, i will show you how to send curl http with the put request in laravel 9, let’s need to update the route file code and controller file code.

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 codingspoint.com',
                    'body' => 'This is test from codingspoint.com as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}
4) Laravel 9 HTTP cURL DELETE Request Example:

Here, i will show you how we can send curl http with the delete request in laravel 9, let’s need to update route file code and controller file code.

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 9 API with Response:

Here i will create a very simple http request with the full example. Just we have to create the simple route to call controller method. so let’s create it:

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

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →