In this tutorial you will learn about the Laravel 9 Guzzle HTTP GET & POST Example and its application with practical example.
In this Laravel 9 Guzzle HTTP Client Requests Example tutorial I’ll show you how to create GET or POST request using Guzzle HTTP Client Requests in laravel 9. In this tutorial you will learn to use Guzzle HTTP Client to create GET and POST request in laravel 9. In this article I will share example to use Guzzle HTTP Client for making GET and POST requests in Laravel 9.
Guzzle HTTP
The guzzle http client is used to call external or internal APIs in your laravel application. The guzzle http client makes it easy to call external APIs in laravel 9 with get and post request.
Laravel 8 Guzzle HTTP GET & POST Example
In this step by step guzzle http GET and POST request laravel 9 tutorial I will demonstrate you how to use http guzzle http client package for calling external or internal apis in your laravel 9. Please follow the instruction given below:
- Install Laravel 9
- Database Configuration
- Install guzzlehttp/guzzle Package
- Make Routes
- Create Controllers By Artisan
- Run Development Server.
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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 |
Install guzzlehttp/guzzle Package
In this step, we will install guzzlehttp/guzzle Package via the composer dependency manager. Use the following command to install guzzlehttp/guzzle Package.
1 |
composer require guzzlehttp/guzzle |
Make Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
use App\Http\Controllers\PostGuzzleController; Route::get('posts',[PostGuzzleController::class,'index']); Route::get('posts/store', [PostGuzzleController::class, 'store' ]); |
Create Controllers by Artisan
Now, lets create a controller named PostGuzzleController using command given below –
1 |
php artisan make:controller PostGuzzleController |
The above command will create PostGuzzleController. Now, go to app/http/controller and open PostGuzzleController.php.Then update the following methods into your controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?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 w3adda.com', 'body' => 'This is test from w3adda.com as body', ]); dd($response->successful()); } } |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 |
http://127.0.0.1:8000/posts http://127.0.0.1:8000/posts/store |