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
- Install Laravel and Basic Configurations
- Install guzzlehttp/guzzle Package
- Create Post Table
- Create Controllers and Model
- Config Controllers
- Create Routes
- 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
<?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:
<?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:
<?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.
Route::get('post','GuzzleController@postRequest');
Route::get('get','GuzzleController@getRequest');
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. ?
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.