Laravel 10 Http Guzzle Request Crud Example

  • 08-03-2023
  • 1193
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Guzzle Http Laravel 10; We will see in this post full CRUD operation using the HTTP Guzzle package, This package is now laravel already by default included in the laravel 10 version. This is helping us when we try to fetch records from the API from the other server. This is helping us when well try to access third-party services that time we must need to use API, and this API will be provided by the service provided. That API we need to integrate into your app. so to execute the API URL from our app we must need to use HTTP guzzle, this package will help us to execute that API and give us a response.

Laravel Guzzle Http also helps us to get authentication API data with the pass token in the header, also we can many things passed with API requests like body payload, and JSON data. header etc...

After executing API the Guzzle HTTP client also gives all the facility to collect data in different methods like an array, JSON, bool, etc.., we will collect data from API and use that according to our way.

Step 1. Install Project

Create a new clone of laravel 10 projects, use the below command, and create a project, make sure your php version is 8.0 or greater to support this laravel version.

composer create-project laravel/laravel example-app

Step 2. Make Controller

Create a controllerm use below given command and create, This command will create a HttpPostController controller. This HTTP guzzle composer laravel already by default gives u, no need to install composer for that.

php artisan make:controller HttpPostController

Follow the below-given controller, This controller CRUD operates all methods given using HTTP guzzle, We will get a record from the API, we will send a post request to the API, we will fetch data from the API using id, and last we will delete that data using delete API in the HTTP Guzle client helper function.

app/Http/Controllers/HttpPostController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class HttpPostController extends Controller
{
    public function index()
    {
        $response = Http::get('https://jsonplaceholder.typicode.com/posts');

        $jsonData = $response->json();

        dd($jsonData);
    }

    public function store()
    {
        $response = Http::post('https://jsonplaceholder.typicode.com/posts',[
                        'title' => 'This is text example from webappfix',
                        'body'  => 'This is text example from webappfix as body'
                    ]);

        $jsonData = $response->json();

        dd($jsonData);
    }

    public function update()
    {
        $response = Http::put('https://jsonplaceholder.typicode.com/posts/1',[
                        'title' => 'This is text example from webappfix',
                        'body'  => 'This is text example from webappfix as body'
                    ]);

        $jsonData = $response->json();

        dd($jsonData);
    }

    public function delete()
    {
        $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');

        $jsonData = $response->json();

        dd($jsonData);
    }
}

Step 3. Routes Define

Create given routes in your web.php file. Open your web.php file and add these four routes to your file.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HttpPostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('post',[HttpPostController::class,'index']);
Route::get('post/store',[HttpPostController::class,'store']);
Route::get('post/update',[HttpPostController::class,'update']);
Route::get('post/delete',[HttpPostController::class,'delete']);

Step 4. Start Development Server

Now we will start the development server, using the below command. This command will start your app local develpment server.

php artisan serve

Copy This given URL and paste it into your web browser, this URL will fetch a record from the API and will display in dump mode in your app.

http://127.0.0.1:8000/post

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :