ItSolutionStuff.com

Laravel Http Curl Post Request with Headers Example

By Hardik Savani • April 16, 2024
Laravel

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...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Http Curl Get Request Example

Read Now →

Laravel 8 Autocomplete Search from Database Example

Read Now →

Laravel 8 Inertia JS CRUD with Jetstream & Tailwind CSS

Read Now →

Laravel 8 Yajra Datatables Example Tutorial

Read Now →

Laravel 8 Pagination Example Tutorial

Read Now →

Laravel 8 Livewire CRUD with Jetstream & Tailwind CSS

Read Now →

Laravel 7 Http Client Request | Laravel 7 Guzzle Http Client Example

Read Now →

Laravel 6 Guzzle Http Client Example

Read Now →

Guzzle http client request tutorial with Laravel 5.8

Read Now →

Laravel 5.7 Guzzle http client POST request example

Read Now →

Laravel Guzzle Http Client POST Request Example

Read Now →