Laravel 8 HTTP cURL POST Request With Headers Example

Laravel 8

Nicesnippets

18-01-2022


Laravel 8 HTTP cURL POST Request With Headers Example

Hi Friends,

I am going to explain you example of Laravel 8 HTTP cURL POST Request with Headers. This tutorial will be easy to understand and implement. It will give you the complete idea of Http curl request integration with headers in laravel 8.

Here, i will give you simple examples of Http facade to work with curl request and it’s methods. Curl request is very useful to send data in various request types. How to send headers with curl request we also see in this article.

We will see cURL request with POST data using HTTP and GuzzleHTTP. To understand cURL we have taken fake data api urls from here Fake API URL.

cURL is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, GOPHER, DICT, TELNET, LDAP or FILE).

Let's see bellow example:

Step : 1 - Installation Fresh Laravel Application


Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog

Step : 2 - Create Controller & Add cURL Concept

php artisan make:controller SiteController

It will create a file SiteController.php at /app/Http/Controllers folder.

Open SiteController.php and write this complete code into it.

cURL Request with POST Data Using HTTP –

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Http;

class SiteController extends Controller

{

public function index()

{

// URL

$apiURL = 'https://jsonplaceholder.typicode.com/posts';

// POST Data

$postInput = [

'title' => 'Sample Post',

'body' => "This is my sample curl post request with data",

'userId' => 22

];

// Headers

$headers = [

//...

];

$response = Http::withHeaders($headers)->post($apiURL, $postInput);

$statusCode = $response->status();

$responseBody = json_decode($response->getBody(), true);

echo $statusCode; // status code

dd($responseBody); // body response

}

}

Output:

array:4[

"title"=>"Sample Post"

"body"=>"This is my sample curl post request with data"

"userId"=>22

"id"=>101

]

cURL Request with POST Data Using GuzzleHttp –

To work with GuzzleHttp, we need to install guzzle package into laravel application. Here is the given command to install guzzlehttp package into application –

Step : 3 - Install guzzlehttp/guzzle Package

Open project into terminal run this command.

composer require guzzlehttp/guzzle

This package will install guzzlehttp and also updates composer.json file at root.

"require": {

//...

"guzzlehttp/guzzle": "^7.0.1",

//...

},

app/Http/Controller/SiteController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Http;

class SiteController extends Controller

{

public function index()

{

// URL

$apiURL = 'https://jsonplaceholder.typicode.com/posts';

// POST Data

$postInput = [

'title' => 'Sample Post',

'body' => "This is my sample curl post request with data",

'userId' => 22

];

// Headers

$headers = [

//...

];

$response = Http::withHeaders($headers)->post($apiURL, $postInput);

$statusCode = $response->status();

$responseBody = json_decode($response->getBody(), true);

echo $statusCode; // status code

dd($responseBody); // body response

}

}

Output:

array:4[

"title"=>"Sample Post"

"body"=>"This is my sample curl post request with data"

"userId"=>22

"id"=>101

]

I hope it can help you...