To make request from the application we can use guzzle http package. It provides various options to manage all the things regarding reuest and response.
In this turorial we wil se how we can make http reuest and get response in laravel 8.
composer create-project --prefer-dist laravel/laravel guzzle_request
composer require guzzlehttp/guzzle
Add this routes in web.php
Route::get('get_request','[email protected]');
Route::post('post_request','[email protected]');
app/Http/Controllers/GuzzleExampleController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class GuzzleExampleController extends Controller
{
public function index()
{
$response = Http::get('https://jsonplaceholder.typicode.com/todos');
$jsonData = $response->json();
dd($jsonData);
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());
}
public function store()
{
$response = Http::post('http://jsonplaceholder.typicode.com/posts', [
'title' => 'guzzle example title',
'body' => 'guzzle example body',
]);
dd($response->successful());
}
}
Step - 5 : Output for get request
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 1,
"id": 3,
"title": "fugiat veniam minus",
"completed": false
},
{
"userId": 1,
"id": 4,
"title": "et porro tempora",
"completed": true
},
...
]
{
"id": 101
}
I hope it will help you.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to remove white space from the beginning of a string in PHP
Use the PHP ltrim() function...How to create custom select box in HTML using CSS and jQuery
Use the CSS :selected Pseudo-class with...How to Set the Value of Input Text Box using jQuery
Use the jQuery val() Method You can s...Python Numpy Array Tutorial Part 2
In the first part, we have discussed wha...How to get the Raw SQL Query from the Laravel Query
Hello guys, Sometimes, you may want t...