How to Use Guzzle HTTP Client Request in Laravel

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:

  1. PATCH
  2. PUT
  3. GET
  4. DELETE
  5. POSTS

Here are the steps to use Guzzle HTTP Client request in Laravel:

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:^7.0

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.

Route::get('post','DataController@postRequest');
Route::get('get','DataController@getRequest');

Step 4: Setup MySQL database For Second Project

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

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 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. So let us do it.

// api.php

Route::post('store', 'GuzzlePostController@store');
Route::get('index', '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

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

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

Guzzle http client GET requestThat’s it!

13 thoughts on “How to Use Guzzle HTTP Client Request in Laravel”

    • 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

      Reply
  1. 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.

    Reply
  2. 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;
    }

    Reply
  3. 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

    Reply

Leave a Comment

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