To use Guzzle HTTP Client request in Laravel, use the “gurzzlehttp/guzzle” package. A Guzzle is a PHP HTTP CLIENT that we use to send HTTP requests for trivial integration with web services, such as:
- PATCH
- PUT
- GET
- DELETE
- POSTS
Step 1: Install Laravel Project
Install the Consuming API Project.
composer create-project --prefer-dist laravel/laravel laravelguzzle
Go into the project folder.
cd laravelguzzle
Start the development server with the following command.
php artisan serve
After successfully installing the first project, we install the second Project.
composer create-project --prefer-dist laravel/laravel laravelguzzlepost
Go into that folder.
cd laravelguzzlepost
We have already started the Laravel development server at port: 8000. So this time, we need to start the server at a different port, and you can change the port using the following command.
php artisan serve --port=8001
Step 2: Install guzzlehttp/guzzle Package
We will install the guzzlehttp/guzzle package by typing the following command in cmd.
composer require guzzlehttp/guzzle
Step 3: Create a Controller For the First Project
php artisan make:controller DataController --resource
It will create one controller file called DataController.php.
We register the first Project route in routes >> web.php file. So let us do it.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\DataController; Route::get('/', function () { return view('welcome'); }); Route::get('get', [DataController::class, 'postRequest'])->name("datacontroller.get"); Route::get('index', [DataController::class, 'getRequest'])->name("datacontroller.index");
Step 4: Setup MySQL database For the Second Project
Configure this database in the .env file.
//.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=8889 DB_DATABASE=laravelguzzle DB_USERNAME=root DB_PASSWORD=root
Step 5: Construct a model and migration file
php artisan make:model GuzzlePost -m
It will create two files.
- GuzzlePost.php model.
- create__guzzle_posts_table migration file.
We need to create Schema for the passports table. So navigate to Laravel >> database >> migrations >> create__guzzle_posts_table.
//create__guzzle_posts_table public function up() { Schema::create('guzzle_posts', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); }
Move to your terminal and hit the following command.
php artisan migrate
Step 6: Create a Controller For the Second Project
php artisan make:controller GuzzlePostController --resource
It will create one controller file called GuzzlePostController.php.
We register the second Project route in routes >> api.php file, but laravel 11 has no route/api.php file
So we need to create by typing the following command in your terminal:
php artisan install:api
Our api.php file looks like this
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\GuzzlePostController; Route::get('/user', function (Request $request) { return $request->user(); })->middleware('auth:sanctum'); Route::post('store', [GuzzlePostController::class, 'store'])->name("guzzlepostcontroller.store"); Route::get('index', [GuzzlePostController::class, 'index'])->name("guzzlepostcontroller.index");
Step 7: Guzzle http client POST request
We will send a post request to the Second Project.
// DataContoller.php public function postRequest() { $client = new \GuzzleHttp\Client(); $response = $client->request('POST', 'http://localhost:8001/api/store', [ 'form_params' => [ 'name' => 'krunal', ] ]); $response = $response->getBody()->getContents(); echo '<pre>'; print_r($response); }
We can store data in the Second Project GuzzlePostController file.
//GuzzlePostController.php use App\Models\GuzzlePost; public function store(Request $request) { $data = new GuzzlePost(); $data->name=$request->get('name'); $data->save(); return response()->json('Successfully added'); }
Move to the browser and hit this URL: http://127.0.0.1:8000/get
Every time you enter the above URL, it returns the same response.
Our database looks like this:
Step 8: Guzzle http client GET request
We have to get the request for the Second Project.
// DataContoller.php public function getRequest() { $client = new \GuzzleHttp\Client(); $request = $client->get('http://localhost:8001/api/index'); $response = $request->getBody()->getContents(); echo '<pre>'; print_r($response); exit; }
We can retrieve data in the index function(second project).
// GuzzlePostController.php public function index() { $data = GuzzlePost::all(); return response()->json($data); }
Move to the browser and hit this URL: http://127.0.0.1:8000/index
Our json response looks like this:
Rustem
Thank you
Jake
I’m unsure of Step 7 what is Datacontroller.php and where is that located?
Thanks.
Krunal
Please refer all the steps, I have already created that DataController.php file. There are two Laravel Projects.
1) For one project, it is DataController
2) For the second project, it is GuzzlePostController
Kuldeep Singh
Hi Krunal,
I am a beginner for Laravel. Could you please guide me how to save data onto server one (not onto server two).
Kind regards.
Anonymouse703
Hi Krunal, I just have little confusion with two servers you’d given… by the way in my case is I just want to list all the hooks from github repo.
What should be the better implementation for this?
Like this? example I have a controller name (Webhook Controller)
public function index()
{
$data = GuzzlePost::all();
return response()->json($data);
}
public function store(Request $request)
{
$data = new GuzzlePost();
$data->name=$request->get(‘name’);
$data->save();
return response()->json(‘Successfully added’);
}
public function postRequest()
{
$client = new \GuzzleHttp\Client();
$response = $client->request(‘POST’, ‘http://localhost:8001/api/store’, [
‘form_params’ => [
‘name’ => ‘krunal’,
]
]);
$response = $response->getBody()->getContents();
echo ”;
print_r($response);
}
public function getRequest()
{
$client = new \GuzzleHttp\Client();
$request = $client->get(‘http://localhost:8001/api/index’);
$response = $request->getBody()->getContents();
echo ”;
print_r($response);
exit;
}
erick
gracias
Diyata Diallo
Merci beaucoup
Shiro
Can help help me answer some questions ?
1. Do you install composer packed “guzzle-laravel” for both project ?
2. Which the library use for the “Request” method? Ex:
—
use Illuminate\Http\Request;
or
use GuzzleHttp\Psr7\Request;
—-
Thx you so much !,Plx
Alexander Yurchenko
Try use AS
use IlluminateHttpRequest AS LaraRequest;
use GuzzleHttpPsr7Request;
yjoy
what will be the route for second project controller
Krunal Lathiya
You need to define routes in the api.php file.
We have mentioned everything above.
ketan
sir your tutorial is best nice post
Krunal Lathiya
Keep learning and sharing!
zankhna
Having issues in DataController for using GuzzelPost model…Any help??
Krunal Lathiya
You should not use the GuzzelPost model in DataController because both projects are different.
Let me know if are still facing a problem.
Gem
where did the response() come from?
Krunal Lathiya
response () comes from the Second project.