Laravel guzzle http client: Complete Guide
In a real-world scenario, we use Guzzle Http Client to send a request from the Laravel server to third-party servers like Github API, Facebook API, Payment apis like Paypal or Stripe. In this example, I am not sending the Network request to any of these APIs, but I will create my Laravel server and communication between those two servers. Thus, it is a kind of server-to-server communication.
Many years ago, we used cURL for the same purpose. But in this example, I am using Guzzle Http Client Library to get work done.
Laravel guzzle http client
The guzzlehttp/guzzle package is used for GET and POST requests. In this example, we use laravel and guzzle. We will create two Laravel projects for this tutorial because the First Laravel project will become a Consumer project, and the second Laravel project will be our Producer Laravel Project.
First, we install two new Laravel projects. One Project is For Consuming API and the Second is For Creating API.
Step 1: Install Laravel Project
Now we 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:~6.0
Step 3: Create Controller For 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.
Route::get('post','DataController@postRequest'); Route::get('get','DataController@getRequest');
Step 4: Setup MySQL database For Second Project
Now, configure this database in the .env file.
//.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravelguzzle DB_USERNAME=root DB_PASSWORD=
Move to your terminal and hit the following command.
php artisan migrate
Step 5: Construct a model and migration file
Type the following command in your terminal.
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(); }); }
Step 6: Create Controller For 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. So let us do it.
//api.php Route::post('store', 'GuzzlePostController@store'); Route::get('index', 'GuzzlePostController@index');
Step 7: Guzzle http client POST request
Next, 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 public function store(Request $request) { $data = new GuzzlePost(); $data->name=$request->get('name'); $data->save(); return response()->json('Successfully added'); }
Step 8: Guzzle http client GET request
Next, 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.
// GuzzlePostController.php public function index() { $data = GuzzlePost::all(); return response()->json($data); }
Finally, Our Laravel guzzle http client Example is over. Thanks for taking it.
Thank you
I’m unsure of Step 7 what is Datacontroller.php and where is that located?
Thanks.
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
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.
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;
}
gracias
Merci beaucoup
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
Try use AS
use IlluminateHttpRequest AS LaraRequest;
use GuzzleHttpPsr7Request;
what will be the route for second project controller
sir your tutorial is best nice post
Having issues in DataController for using GuzzelPost model…Any help??
where did the response() come from?