Hello Friends,
In this tutorial, I am going to learn you laravel 8 guzzle http client example. We will look at example of laravel 8 http guzzle request example. Here you will learn how to create http guzzle request example in laravel 8. it's simple example of laravel 8 guzzle http client example.
This tutorial will give you example of http guzzle request example in laravel 8. you can understand concept of http guzzle request in laravel 8.
Here i will example of http guzzle request example in laravel 8. So let's see the bellow example:
Install guzzlehttp/guzzle
In this step we have to need guzzle http composer package. you will install guzzlehttp/guzzle package using bellow command:
composer require guzzlehttp/guzzle
Simple Example:
Here we will create easy and simple http request example. we need to create simple route to call controller method:
routes/web.php
use App\Http\Controllers\PostController;
Route::get('posts',[PostController::class,'index']);
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PostController extends Controller
{
public function index()
{
$response = Http::get('http://jsonplaceholder.typicode.com/posts');
$jsonData = $response->json();
dd($jsonData);
}
}
Output :
Http Post Request Example:
We will create very simple http request full example. we need to create simple route to call controller method. so let's create it:
routes/web.php
use App\Http\Controllers\PostController;
Route::get('posts/store', [PostController::class, 'store' ]);
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PostController extends Controller
{
public function store()
{
$response = Http::post('http://jsonplaceholder.typicode.com/posts', [
'title' => 'This is test from NiceSnippets.com',
'body' => 'This is test from NiceSnippets.com as body',
]);
dd($response->successful());
}
}
Example with Response:
Here we will create easy and simple http request with response example. we need to create simple route to call controller method:
routes/web.php
use App\Http\Controllers\PostController;
Route::get('posts2',[PostController::class,'index2']);
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PostController extends Controller
{
public function index2()
{
$response = Http::get('http://jsonplaceholder.typicode.com/posts');
$jsonData = $response->json();
echo "<pre> status:";
print_r($response->status());
echo "<br/> ok:";
print_r($response->ok());
echo "<br/> successful:";
print_r($response->successful());
echo "<br/> serverError:";
print_r($response->serverError());
echo "<br/> clientError:";
print_r($response->clientError());
echo "<br/> headers:";
print_r($response->headers());
}
}
Output :
status:200
ok:1
successful:1
serverError:
clientError:
headers:Array
(
[Date] => Array
(
[0] => Thu, 12 Mar 2020 06:08:58 GMT
)
[Content-Type] => Array
(
[0] => application/json; charset=utf-8
)
[Transfer-Encoding] => Array
(
[0] => chunked
)
.....
)
You can also get more information about Http Client in Laravel Docs: Click Here
It will help you....