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

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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