Laravel 5.7 Guzzle http client POST request example
Here, I will describe how to use guzzle in laravel 5.7 application. i will show you some example of http POST request, GET request, PATCH request, DELETE request using guzzle in laravel 5.7.
A Guzzle is a PHP HTTP client that makes it easy to send HTTP requests with data, headers and trivial to integrate with web services. Guzzle is a simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc. Guzzle can also send both synchronous and asynchronous requests using the same interface.
So, if you want to use http guzzle in laravel 5.7 then you have to just follow few step to get simple example.
Install Package:
now we will install guzzlehttp/guzzle package and then we can easily use thir method So let's just run bellow command.
composer require guzzlehttp/guzzle
Example of Requests Using Guzzle:
Now here i will show you how to run all above listed request you can use following controller method:
GET Request:
public function getGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$request = $client->get('http://myexample.com');
$response = $request->getBody();
dd($response);
}
POST Request:
public function postGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$url = "http://myexample.com/api/posts";
$myBody['name'] = "Demo";
$request = $client->post($url, ['body'=>$myBody]);
$response = $request->send();
dd($response);
}
PUT Request:
public function putGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$url = "http://myexample.com/api/posts/1";
$myBody['name'] = "Demo";
$request = $client->put($url, ['body'=>$myBody]);
$response = $request->send();
dd($response);
}
DELETE Request:
public function deleteGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$url = "http://myexample.com/api/posts/1";
$request = $client->delete($url);
$response = $request->send();
dd($response);
}
As above example, you can see how it works.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Google ReCaptcha in Laravel?
- Laravel 5.7 - Generate PDF from HTML Example
- Laravel 5.7 - Create REST API with authentication using Passport Tutorial
- How to send mail using queue in Laravel 5.7?
- Laravel 5.7 - Pagination Link Customizations Example
- Laravel 5.7 Autocomplete Search from Database using Typeahead JS
- Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example