Skip to content

All topics  /  Laravel

Laravel 10 HTTP cURL POST Request With Headers Example

Laravel ·

Teams applying “Laravel 10 HTTP cURL POST Request With Headers Example” in a production project can use the operational overview to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi Friends,

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

Here, I will give you simple examples of HTTP facades to work with curl requests and their methods. Curl request is very useful to send data in various request types. How to send headers with curl requests we saw in this article.

We will see cURL requests 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: Download Laravel


Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step : 2 - Create Controller & Add cURL Concept

php artisan make:controller SiteController

It will create a file SiteController.php in the /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 the Guzzle package into the Laravel application. Here is the given command to install guzzlehttp package into the application –

Step : 3 - Install guzzlehttp/guzzle Package

Open the project into the terminal and run this command.

composer require guzzlehttp/guzzle

This package will install guzzlehttp and also updates the composer.json file at the 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
]