Laravel Http Curl Post Request with Headers Example

By Hardik Savani April 16, 2024 Category : 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 :
Shares