In this tutorial you will learn about the Laravel 7 Guzzle HTTP Client Requests Example and its application with practical example.
In this Laravel 7 Guzzle HTTP Client Requests Example tutorial I’ll show you how to implement or use Guzzle HTTP Client Requests in laravel. In this tutorial you will learn to use Guzzle HTTP Client Requests in laravel.
Laravel 7 Guzzle HTTP Client Requests Example
- Step 1: Install Laravel New App
- Step 2: Add Database Details
- Step 3: Install guzzlehttp/guzzle Package
- Step 4: Create Model and Migration
- Step 5: Add Routes
- Step 6: Create Controllers By Artisan
- Step 7: Run Development Server
Step 1: Install Laravel New App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Add Database Details
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Step 3: Install guzzlehttp/guzzle Package
In this step, we will install guzzlehttp/guzzle Package via the composer dependency manager. Use the following command to install guzzlehttp/guzzle Package.
1 |
composer require guzzlehttp/guzzle |
Step 4: Create Modal and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan nake:modal Post -m |
Navigate database/migrations/ and open create_posts_table.php file. Then update the following code into this file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->integer('user_id'); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 5: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 |
Route::get('post','GuzzleController@postRequest'); Route::get('get','GuzzleController@getRequest'); Route::post('store','PostController@store'); Route::get('get','PostController@get'); |
Step 6: Create Controllers by Artisan
Now, lets create a controller named PostController and GuzzleController using command given below –
1 2 3 |
php artisan make:controller PostController php artisan make:controller GuzzleController |
This command will create PostController and GuzzleController by the artisan command.
Next, Navigate to app/http/controller and open PostController.php.Then update the following methods into your controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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); } } |
After that, Navigate to app/http/controller and open GuzzleController.php.Then update the following methods into your controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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); } } |
Step 7: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 |
http://localhost:8000/get http://localhost:8000/post |
Note that, you can also use guzzle http put and delete request in laravel apps as follow:
PUT REQUEST
1 2 3 4 5 6 7 8 9 10 |
public function putGuzzleRequest() { $client = new \GuzzleHttp\Client(); $url = "http://example.com/api/posts/1"; $data['name'] = "codechief"; $request = $client->put($url, ['body'=>$data]); $response = $request->send(); dd($response); } |
DELETE REQUEST:
1 2 3 4 5 6 7 8 9 |
public function deleteGuzzleRequest() { $client = new \GuzzleHttp\Client(); $url = "http://example.com/api/posts/1"; $request = $client->delete($url); $response = $request->send(); dd($response); } |