Laravel Http Curl Post Request with Headers Example
Are you looking for example of laravel http curl post request example. This article will give you simple example of laravel curl post request with headers example. you will learn laravel http request post parameters. This article goes in detailed on how to call curl post request in laravel. follow bellow step for how to post curl request in php laravel.
Here, i will give you two examples of how to call curl post request with laravel GuzzleHttp. first example will with http and second example with GuzzleHttp. so let's see both examples one by one here. you can easily use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Install guzzlehttp/guzzle Package:
you have to install guzzlehttp/guzzle composer package in your project:
composer require guzzlehttp/guzzle
Example 1:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$apiURL = 'https://api.mywebtuts.com/api/users';
$postInput = [
'first_name' => 'Hardik',
'last_name' => 'Savani',
'email' => 'example@gmail.com'
];
$headers = [
'X-header' => 'value'
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->status();
$responseBody = json_decode($response->getBody(), true);
dd($responseBody);
}
}
Output
Array
(
[id] => 281
[first_name] => Hardik
[last_name] => Savani
[email] => example@gmail.com
[created_at] => 2021-07-29T03:51:48.693210Z
)
Example 2:
<?php
namespace App\Http\Controllers;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index2()
{
$apiURL = 'https://api.mywebtuts.com/api/users';
$postInput = [
'first_name' => 'Hardik',
'last_name' => 'Savani',
'email' => 'example@gmail.com'
];
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $apiURL, ['form_params' => $postInput]);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
dd($responseBody);
}
}
Output
Array
(
[id] => 281
[first_name] => Hardik
[last_name] => Savani
[email] => example@gmail.com
[created_at] => 2021-07-29T03:51:48.693210Z
)
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
- Laravel Http Curl Get Request Example
- Laravel 8 Autocomplete Search from Database Example
- Laravel 8 Inertia JS CRUD with Jetstream & Tailwind CSS
- Laravel 8 Yajra Datatables Example Tutorial
- Laravel 8 Pagination Example Tutorial
- Laravel 8 Livewire CRUD with Jetstream & Tailwind CSS
- Laravel 7 Http Client Request | Laravel 7 Guzzle Http Client Example
- Laravel 6 Guzzle Http Client Example
- Guzzle http client request tutorial with Laravel 5.8
- Laravel 5.7 Guzzle http client POST request example
- Laravel Guzzle Http Client POST Request Example