Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

Laravel Guzzle HTTP Client Requests

Published on April 13, 2019 1 min read

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','[email protected]');
Route::get('get','[email protected]');
routes/api.php
Route::post('store','[email protected]');
Route::get('get','[email protected]');

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. ๐Ÿ™‚

You're welcome to suggest any article to write!

Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

2 Replies to “Laravel Guzzle HTTP Client Requests”

  1. Very good article about guzzle, but I have question. How do I insert json data from the database to be use part of the url? In order to create a new data from the guzzle request. Hope a reply from you soon ๐Ÿ™‚

    1. Hi Lisixopo,

      You can insert JSON data to the database by encoding as string. Here’s the way to encode data to pass via guzzle or curl request: $data = json_encode($data). Then just pass the encoded JSON variable $data like other variables in the guzzle request. In the database, you can set the “TEXT” data type of the column.

      To decode the encoded JSON, you need to run this $data = json_decode($data).

      Thank you. ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *