Laravel guzzle HTTP client request example. Here, you will learn how to use guzzle HTTP client get, post, put, and delete requests in laravel apps.
Sometimes, you need to call external or internal APIs in your laravel apps. At that time you can use HTTP guzzle client request in laravel. This tutorial will help to call external or internal URL in your laravel apps by using guzzle client request packages.
Laravel Guzzle HTTP Client Requests
Follow the below steps and use guzzle HTTP client requests in laravel apps:
- 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, Open your terminal and run the following command to download or install laravel fresh new setup:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Add Database Details
After that, open “.env” file and update the database name, username, and password in the env file:
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, Install guzzlehttp/guzzle via composer package. So open your terminal and run the following command:
composer require guzzlehttp/guzzle
Step 4: Create Modal and Migration
In this step, you need to create post table migration and create Post Modal using bellow command:
php artisan nake:modal Post -m
Navigate database/migrations/ and open create_posts_table.php file. Then update the following code into this file:
<?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 following command
php artisan migrate
After you have to put bellow code in your Post model file for create posts table.
Step 5: Add Routes
Next step, Navigate to “routes/web.php” file and add the following routes into your web.php file:
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
Next step, open your terminal and run the following commands:
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:
<?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:
<?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
In this step, use the following php artisan serve command to start your server locally:
php artisan serve
After that, call the above method with parameters on postman by using the following urls:
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
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:
public function deleteGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$url = "http://example.com/api/posts/1";
$request = $client->delete($url);
$response = $request->send();
dd($response);
}
Conclusion
In this tutorial, you have learned how to use guzzle HTTP client requests in laravel apps by using laravel guzzle package.