ItSolutionStuff.com

Laravel Guzzle Http Client POST Request Example

By Hardik Savani • November 5, 2023
Laravel JSON

A very few days ago i was working on my laravel 5.3 application and i require to use WordPress API. I was thinking how to use WP API, But i found docs for WordPress API, But i don't know how to fire GET, POST, PUT and DELETE request from Laravel side. I did search lot but nothing to find good.

However, I found guzzlehttp package for laravel and little bit read about that, i understand we can simply fire get, post, delete etc request from laravel using guzzlehttp client through. But i was also one query how to make basic auto with POST request but here also solution for this.

So in this post, we learn how to make post request from laravel using http guzzle client. In this example i also added basic authentication for example. It is pretty simple, after this example you can simply use other Website APIs too.

So, For GuzzleHttp, we require "guzzlehttp/guzzle" composer package on our laravel application, Let's install this package by using following command:

Install guzzlehttp/guzzle package

composer require guzzlehttp/guzzle:~5.0

Ok, now we are ready to use "GuzzleHttp\Client" Class on Laravel application, So in route file we can make simple POST request like this way, It is for your understanding, you can call your api instead of "http://wp.dev/index.php/wp-json/wp/v2/posts", Let's see:

Add In Your Route File

Route::get('guzzle-http-post-request', function()

{

$body['title'] = "Body Title";

$body['content'] = "Body Description";


$client = new \GuzzleHttp\Client();

$url = "http://wp.dev/index.php/wp-json/wp/v2/posts";


$response = $client->createRequest("POST", $url, ['auth' => ['root','root'],'body'=>$body]);


$response = $client->send($response);


dd($response);

});

You simple run GET, PUT and DELETE request like this way.

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 Where Clause with Function Query Example

Read Now →

How to install and use Image Intervention in Laravel?

Read Now →

Laravel Google Pie Chart Example Tutorial

Read Now →

How to Get Current Week Records in Laravel?

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

How to Rollback Migration in Laravel?

Read Now →

Laravel Eloquent When Condition Example

Read Now →

Laravel Blade Include File If Exists Example

Read Now →

Laravel Livewire Delete Confirmation Example

Read Now →

How to Get Last 30 Days Record in Laravel?

Read Now →

Laravel Carbon diffForHumans() Example

Read Now →

Laravel Twitter API using Thujohn/twitter Tutorial

Read Now →

Laravel Image Resize & Upload with Intervention Image Example

Read Now →