Laravel 10 PHP Guzzle Http Client POST & Get Examples

Laravel 10 PHP Guzzle Http Client POST & Get Examples

Guzzle is a powerful HTTP client package that simplifies the process of calling external or internal APIs in your Laravel 10 web applications. In this tutorial, you will explore how to create and use HTTP GET and POST requests using Guzzle to call external and internal apis with http client post json api in Laravel 10.

When making POST requests with Guzzle, if you are sending POST fields without any files, the Content-Type header will be set to application/x-www-form-urlencoded. However, if you include files in your POST json request, the Content-Type header will be set to multipart/form-data.

Calling external or internal APIs can be a common requirement in Laravel 10 applications, and Guzzle provides an efficient way to handle these requests. By utilizing the Guzzle HTTP client package, you can easily interact with APIs using GET and POST requests.

Laravel 10 PHP Guzzle Http Client POST & Get Examples

By using the following steps, you can use guzzle http client GET & POST json body request to call external and internal apis in laravel 10 apps:

  • Step 1: Create New Laravel 10 Project
  • Step 2: Setup Database to Laravel Project
  • Step 3: Install and Setup guzzlehttp/guzzle Package
  • Step 4: Define Http Guzzle Get & Post Request Routes
  • Step 5: Create Controllers By Artisan
  • Step 6: Run Development Server

Step 1: Create New Laravel 10 Project

First of all, Open your command prompt or terminal.

Then execute the following command into it to download and install laravel fresh new project in your server:

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

Step 2: Setup Database to Laravel Project

Once you have installed laravel project in your server. Now you need to setup database with laravel project.

So, visit your laravel project root directory and open .env file. And then setup database like following:

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 and Setup guzzlehttp/guzzle Package

In this step, Open again your cmd or terminal and execute the following command into it to Install guzzlehttp/guzzle via composer package:

composer require guzzlehttp/guzzle

Step 4: Define Http Guzzle Get & Post Request Routes

Next step, Navigate 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

Next step, open your terminal or cmd. And execute the following command into it to create controller:

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

Step 6: Run Development Server

In this step, execute the following php artisan serve command on cmd or terminal to start your server:

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

By the end of this tutorial, you will have a solid understanding of how to use Guzzle HTTP client to handle GET and POST requests in Laravel 10, enabling you to seamlessly integrate external or internal APIs into your applications.

Recommended Laravel Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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