- Http Client => Laravel 7+
- GuzzleHttp => Old Laravel Version < 7
Performing HTTP Request with Http Client in Laravel
To perform HTTP requests with Laravel Http Client you can directly import and use the facade right away like below. The Http facade comes with "get", "post", "put", "patch" and "delete" methods as follows.
<?php use Illuminate\Support\Facades\Http; $response = Http::get('https://test.com'); $response = Http::post('https://test.com'); $response = Http::put('https://test.com'); $response = Http::patch('https://test.com'); $response = Http::delete('https://test.com');
<?php $response->body() : string; $response->json() : array|mixed; $response->object() : object; $response->collect() : Illuminate\Support\Collection; $response->status() : int; $response->ok() : bool; $response->successful() : bool; $response->failed() : bool; $response->serverError() : bool; $response->clientError() : bool; $response->header($header) : string; $response->headers() : array;
composer require guzzlehttp/guzzle
Performing HTTP Request with GuzzleHttp in Laravel
The 2nd method is to use the GuzzleHttp client and you can install it using composer.
composer require guzzlehttp/guzzle
<?php $client = new GuzzleHttp\Client();
$res = $client->request('POST', 'https://test.com', [ 'form_params' => [ 'your' => 'form-param', ] ]); // If the status code is 200 theng et the body contents if ($res->getStatusCode() == 200) { $response_data = $res->getBody()->getContents(); }
Leave a reply