Laravel 7.x Http Client Request Example

In this Laravel 7 guzzle http clinet request example tutorial, i will discuss how we can use laravel 7 http client request. In this example tutorial you will learn laravel 7 http client request. 

Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing you to quickly make outgoing HTTP requests to communicate with other web applications. This is going to be a simple example of laravel 7 HTTP Client post. 

To make requests, you may use the getpostputpatch, and delete methods. First, let's examine how to make a basic GET request: Laravel 7 provide built in HTTP Client request using guzzlehttp/guzzle package. You can easily run http client request using Http facade from the version of Laravel 7. 

Here, i will give you a simple demo exmaple. So that you can learn how to use HTTP facades in client request. Let's see the example of Laravel 7 http client rrquest.

 

Install guzzlehttp/guzzle

Before using http client we need to install Guzzle package as a dependency of your application. So install it by the below command.

composer require guzzlehttp/guzzle

 

GET REQUEST

use Illuminate\Support\Facades\Http;

$response = Http::get('http://test.com');

 

GET Request Query Parameters

$response = Http::get('http://test.com/users', [
    'name' => 'Taylor',
    'page' => 1,
]);

 

POST REQUEST

$response = Http::post('http://test.com/users', [
    'name' => 'Steve',
    'role' => 'Network Administrator',
]);

 

Multi-Part Requests

If you would like to send files as multi-part requests, you should call the attach method before making your request. 

$response = Http::attach(
    'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://test.com/attachments');

Instead of passing the raw contents of a file, you may also pass a stream resource:

$photo = fopen('photo.jpg', 'r');

$response = Http::attach(
    'attachment', $photo, 'photo.jpg'
)->post('http://test.com/attachments');

 

Read also : Laravel 7.x Guzzle Http Client Request Example

 

Hope it can help you.