Laravel 9 Guzzle HTTP GET & POST Request Tutorial

Laravel 9 http guzzle get and post request; Through this tutorial, i am going to show you how to use http Guzzle client GET and POST requests in PHP Laravel 9 apps.

In this tutorial, i will call external or internal APIs in your laravel 9 apps using HTTP guzzle client request. which makes it easy to call external APIs in laravel 9 with get and post request.

Laravel 9 Guzzle HTTP GET & POST Request Tutorial

Follow the below given steps to use guzzle HTTP client get and post request in laravel 9 apps:

  • Step 1: Install Laravel 9 App
  • Step 2: Database Configuration
  • Step 3: Install guzzlehttp/guzzle Package
  • Step 4: Make Routes
  • Step 5: Create Controllers By Artisan
  • Step 6: Run Development Server

Step 1: Install Laravel 9 App

Run the following command on command prompt to download or install laravel fresh new setup:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

After that, open “.env” file and update the database name, username, and password in the env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password

Step 3: Install guzzlehttp/guzzle Package

To install guzzlehttp/guzzle via composer package. So open your terminal and run the following command:

composer require guzzlehttp/guzzle

Step 4: Make Routes

Then visit to “routes/web.php” file and add the following routes into your web.php file:

use App\Http\Controllers\PostGuzzleController;

Route::get('posts',[PostGuzzleController::class,'index']);
Route::get('posts/store', [PostGuzzleController::class, 'store' ]);

Step 5: Create Controllers by Artisan

Run the following command on command prompt to create controller file:

php artisan make:controller PostGuzzleController

This command will create PostGuzzleController by the artisan command.

After that, Navigate to app/http/controller and open PostGuzzleController.php.Then update the following methods into your controller file:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PostGuzzleController extends Controller
{
    public function index()
    {
        $response = Http::get('http://jsonplaceholder.typicode.com/posts');
  
        $jsonData = $response->json();
        
        dd($jsonData);
    }
    public function store()
    {
        $response = Http::post('http://jsonplaceholder.typicode.com/posts', [
                    'title' => 'This is test from laratutorials.com',
                    'body' => 'This is test from laratutorials.com as body',
                ]);
  
        dd($response->successful());
    }
}

Step 6: Run Development Server

Run php artisan serve command on command prompt to start your server locally:

php artisan serve

After that, call the above method with parameters on postman by using the following urls:

http://127.0.0.1:8000/posts
http://127.0.0.1:8000/posts/store

Conclusion

Laravel 9 HTTP guzzle client request tutorial, you have learned how to use guzzle HTTP client requests in laravel 9 apps by using laravel guzzle package.

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *