Laravel 9 HTTP cURL POST Request with Headers Tutorial

Share this Article
Reading Time: 5 minutes
617 Views

Inside this article we will see the concept of Laravel 9 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 9.

In laravel we will use 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).

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Controller & Add cURL Concept

Next,

We need to create a controller file.

$ php artisan make:controller SiteController

It will create a file SiteController.php inside /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://api.onlinewebtutorblog.com/employees';

        // POST Data
        $postInput = [
            "username" => "sanjay_owt",
            "name" => "Sanjay Kumar",
            "email" => "sanjay@example.net",
            "gender" => "Male",
            "designation" => "Web Developer",
            "phone_number" => "1234567890",
            "complete_address" => "Sample, Test Address"
        ];
  
        $response = Http::withHeaders($headers)->post($apiURL, $postInput);
  
        $statusCode = $response->status();
        $responseBody = json_decode($response->getBody(), true);
      
        echo "Status code: ". $statusCode;  // status code

        dd($responseBody); // body response
    }
}

Output

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 –

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.2",
        //...
  },
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        // URL
        $apiURL = 'https://api.onlinewebtutorblog.com/employees';

        // POST Data
        $postInput = [
            "username" => "sanjay_owt",
            "name" => "Sanjay Kumar",
            "email" => "sanjay@example.net",
            "gender" => "Male",
            "designation" => "Web Developer",
            "phone_number" => "1234567890",
            "complete_address" => "Sample, Test Address"
        ];
  
        // Headers
        $headers = [
            //...
        ];
  
        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', $apiURL, ['form_params' => $postInput, 'headers' => $headers]);
     
        $responseBody = json_decode($response->getBody(), true);
      
        echo "Status code: ". $response->getStatusCode(); // status code
    
        dd($responseBody); // body response
    }
}

Output

We hope this article helped you to learn Laravel 9 Http cURL Post Request with Headers Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5