AppDividend
Latest Code Tutorials

Laravel guzzle http client Example

7,151

Get real time updates directly on you device, subscribe now.

Laravel guzzle http client Example is the topic, we will discuss today. We use guzzlehttp/guzzle Package for http client GET and POST request. In this example, we use laravel 5.6 and guzzle 6. We will create two Laravel projects for this tutorial because First Laravel project will become a Consumer project and second Laravel project will be our Producer Laravel Project.

In a real-world scenario, we use Guzzle Http Client to send a request from Laravel server to third-party servers like Github API, Facebook API, Payment API like Paypal or Stripe. In this example, I am not sending the Network request to any of these APIs, but I will create my own another Laravel server and communicate between those two servers. It is 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 Example

First, we install two new Laravel Project. One Project is For Consuming API and Second is For Creating API.

Step 1: Install Laravel Project

 Now we install 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

Now, we have already started the Laravel development server at the port: 8000. So this time, we need to start the server at the 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 guzzlehttp/guzzle package by typing 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 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.

  1. GuzzlePost.php model.
  2. 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 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 have post request to Second Project.

Related Posts
1 of 39
// 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  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');

    }

Guzzle http client POST request

Step 8: Guzzle http client GET request

Next, we have to get the request to 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 index function.

// GuzzlePostController.php

public function index()
    {
        $data = GuzzlePost::all();
        return response()->json($data);
    }

Guzzle http client GET request

Finally, Our Laravel guzzle http client Example is over. Thanks for taking.

4 Comments
  1. Rustem says

    Thank you

  2. Jake says

    I’m unsure of Step 7 what is Datacontroller.php and where is that located?

    Thanks.

    1. Krunal says

      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

  3. Kuldeep Singh says

    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.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.