In this tutorial you will learn about the Laravel 8 Guzzle Http Client Request Example and its application with practical example.
In this Laravel 8 Guzzle Http Client Request Example tutorial I’ll show you how to create GET or POST request using Guzzle HTTP Client Requests in laravel 8. In this tutorial you will learn to use Guzzle HTTP Client to create GET and POST request in laravel. In this article I will share example to use Guzzle HTTP Client for making GET and POST requests in Laravel 8.
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 8 with get and post request.
Laravel 8 Guzzle Http Client Request Example
In this step by step tutorial you will learn Laravel 8 Guzzle Http Client Request Example. Please follow instruction given below:
Create Routes
First of all we need to define routes in “routes/web.php” file for GET Request example. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; /* |-------------------------------------------------------------------------- | Web Route |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('posts', [PostController::class, 'index']); |
Create Controller By Artisan Command
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller RazorpayController |
Once the above command executed, it will create a controller file PostController.php in app/Http/Controllers/ directory. Open the PostController.php file and put the following code in it.
app/Http/Controllers/PostController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class PostController extends Controller { public function index() { $response = Http::get('http://jsonplaceholder.typicode.com/posts'); $jsonData = $response->json(); dd($jsonData); } } |
Http Post Request Example:
Now we need to define routes in “routes/web.php” file for POST Request example. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('posts/store', [PostController::class, 'store']); |
app/Http/Controllers/PostController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class PostController extends Controller { 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()); } } |
Example with Response:
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('posts', [PostController::class, 'index']); |
app/Http/Controllers/PostController.php
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 29 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class PostController extends Controller { public function index() { $response = Http::get('http://jsonplaceholder.typicode.com/posts'); $jsonData = $response->json(); echo "<pre> status:"; print_r($response->status()); echo "<br/> ok:"; print_r($response->ok()); echo "<br/> successful:"; print_r($response->successful()); echo "<br/> serverError:"; print_r($response->serverError()); echo "<br/> clientError:"; print_r($response->clientError()); echo "<br/> headers:"; print_r($response->headers()); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
status:200 ok:1 successful:1 serverError: clientError: headers:Array ( [Date] => Array ( [0] => Thu, 12 Mar 2020 06:08:58 GMT ) [Content-Type] => Array ( [0] => application/json; charset=utf-8 ) [Transfer-Encoding] => Array { [0] => chunked ) ..... ) |