Laravel Guzzle HTTP Client Requests

We know Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Today, we will discuss Guzzle HTTP client requests in Laravel. Just follow these steps:

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Install guzzlehttp/guzzle Package
  3. Create Post Table
  4. Create Controllers and Model
  5. Config Controllers
  6. Create Routes
  7. Testing

Step 1 : Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

Step 2 : Install guzzlehttp/guzzle Package

Install guzzlehttp/guzzle package by typing following command:

composer require guzzlehttp/guzzle

Step 3 : Create Post Table

Let’s create a posts table to store posts via guzzle:

php artisan make:migration create_posts_table --create=posts

Go  to Laravel  >>  database  >>  migrations  >>  create_posts_table.php

create_posts_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Now migrate database:

php artisan migrate

Step 4 : Create Controllers and Model

We are going to create two controllers. One for posts and another for guzzle requests. You can also create two different projects to test this. Let’s create the controllers:

php artisan make:controller PostController
php artisan make:controller GuzzleController

Create a post model:

php artisan make:model Post

Step 5 : Config Controllers

Open app/Http/Controllers/GuzzleController.php and paste this code:

GuzzleController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class GuzzleController extends Controller
{
    public function postRequest()
    {
        $client = new \GuzzleHttp\Client(['verify' => false]);

        $response = $client->request('POST', 'http://localhost:8000/api/store', [
            'form_params' => [
                'title' => 'Post 1',
            ]
        ]);
        $response = $response->getBody()->getContents();
        echo '<pre>';
        print_r($response);
    }

    public function getRequest()
    {
        $client = new \GuzzleHttp\Client(['verify' => false]);

        $request = $client->get('http://localhost:8000/api/get');
        $response = $request->getBody()->getContents();
        echo '<pre>';
        print_r($response);

    }
}

Open app/Http/Controllers/PostController.php and paste this code:

PostController.php
<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function store(Request $request)
    {
        $data = new Post();
        $data->title = $request->get('title');
        $data->save();
        return response()->json('Successfully added');

    }

    public function get(Request $request)
    {
        $data = Post::all();
        return response()->json($data);
    }
}

Step 6 : Create Routes

We will create web and api routes. Web routes for GuzzleController and API routes for PostController.

routes/web.php
Route::get('post','GuzzleController@postRequest');
Route::get('get','GuzzleController@getRequest');
routes/api.php
Route::post('store','PostController@store');
Route::get('get','PostController@get');

Step 7 : Testing

We have completed add tasks. It’s time to test. Open a browser and visit your Laravel project. I’m using a custom domain (Custom Domain with SSL). So that I’m visiting https://laravel.dev/post to post data via Guzzle. Your URL should like http://localhost:8000/post. I’ve seen ‘Successfully added’ message. I’ve taken a look at the database.

Now, retrieve the data by visiting http://localhost:8000/get

Thank you. ?