ItSolutionStuff.com

Laravel 8 Guzzle Http Client Request Example

By Hardik Savani β€’ November 5, 2023
Laravel

I am going to show you example of laravel 8 http client request example. let’s discuss about laravel 8 guzzle http client example. you will learn php laravel 8 http client request. you'll learn laravel 8 http Client post request.

Laravel 8 provide inbuilt HTTP Client using guzzlehttp/guzzle package. you can easily run http client request using Http facade. you can send GET, POST, PUT, DELETE request with you can easily get response with text and json too. you can also pass header and authentication token easily.

Here, i will give you very simple example with output. you can see bellow example so you can easily understand how it works:

Simple Example:

We will create very simple http request full example. we need to create simple route to call controller method. so let's create it:

routes/web.php

<?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

<?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);

}

}

Output:

Http Post Request Example:

We will create very simple http request full example. we need to create simple route to call controller method. so let's create it:

routes/web.php

<?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

<?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 ItSolutionStuff.com',

'body' => 'This is test from ItSolutionStuff.com as body',

]);

dd($response->successful());

}

}

Example with Response:

We will create very simple http request full example. we need to create simple route to call controller method. so let's create it:

routes/web.php

<?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

<?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:

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

)

.....

)

You can also get more information about Http Client in Laravel Docs: Click Here.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel 8 Livewire CRUD with Jetstream & Tailwind CSS

Read Now β†’
β˜…

Laravel 8 Send Mail using Gmail SMTP Server

Read Now β†’
β˜…

Laravel 8 Mail | Laravel 8 Send Email Tutorial

Read Now β†’
β˜…

Laravel 8 Multiple File Upload Example

Read Now β†’
β˜…

Laravel 8 PDF | Laravel 8 Generate PDF File using DomPDF

Read Now β†’
β˜…

Laravel 8 Auth with Inertia JS Jetstream Tutorial

Read Now β†’
β˜…

Laravel 8 Database Seeder Tutorial Example

Read Now β†’
β˜…

Laravel 8 Auth with Livewire Jetstream Tutorial

Read Now β†’
β˜…

Laravel 8 Authentication using Jetstream Example

Read Now β†’
β˜…

Laravel 8 File Upload Example Tutorial

Read Now β†’
β˜…

Laravel 8 Create Custom Helper Functions Tutorial

Read Now β†’
β˜…

Laravel 8 Multiple Image Upload Tutorial

Read Now β†’