Laravel 10 HTTP cURL POST Request With Header

In this article, we will see the laravel 10 HTTp cURL POST request with a header. Here, we will learn about how to send HTTP POST requests using cURL in laravel 10. cURL is a computer software project providing a library and command-line tool for transferring data using various network protocols. The name stands for "Client for URL".

Curl (client URL) is a command-line tool powered by the libcurl library to transfer data to and from the server using various protocols, such as HTTP, HTTPS, FTP, FTPS, IMAP, IMAPS, POP3, POP3S, SMTP, and SMTPS. We will see cURL requests with POST data using HTTP and GuzzleHTTP.

So, let's see how to send a POST Request using cURL in laravel 10, laravel 10 sends a POST request using cURL with a header, and php cURL POST request with headers.

Step 1: Download Laravel 10

In this step, we will install laravel 10 using the following command.

composer create-project laravel/laravel example-app

 

Step 2: Create Controller & Add cURL Concept

Then, we will create a controller and add the cURL code to that controller.

php artisan make:controller UsersController

app/Http/Controllers/UsersController.php

cURL Request with POST Data Using HTTP

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class UsersController extends Controller
{
    public function index()
    {
        // URL
        $apiURL = 'https://jsonplaceholder.typicode.com/posts';

        // POST Data
        $postInput = [
            'title' => 'Laravel 10 cURL Example',
            'body' => "This is cURL Post Request Example",
            'userId' => 1
        ];
  
        // 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"=>"Laravel 10 cURL Example"
    "body"=>"This is cURL Post Request Example"
    "userId"=>1
    "id"=>7
]

 

Step 3: Install guzzlehttp/guzzle Package

In this example, we will install the Guzzle package into the Laravel 10 application using the following command.

cURL Request with POST Data Using GuzzleHttp

composer require guzzlehttp/guzzle

Also updates the composer.json file at the root file.

"require": {
    //...
    "guzzlehttp/guzzle": "^7.0.1",
    //...
},

app/Http/Controller/UsersController.php

Headers may be added to requests using the withHeaders method. This withHeaders method accepts an array of key / value pairs

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class UsersController extends Controller
{
    public function index()
    {
        // URL
        $apiURL = 'https://jsonplaceholder.typicode.com/posts';

        // POST Data
        $postInput = [
            'title' => 'How to send HTTP cURL POST Request in Laravel 10',
            'body' => "Laravel 10 cURL Body",
            'userId' => 5
        ];
  
        // Headers
        $headers = [
          'X-First' => 'foo',
          'X-Second' => 'bar'
        ];
  
        $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" => "How to send HTTP cURL POST Request in Laravel 10",
    "body" => "Laravel 10 cURL Body",
    "userId" => 5
    "id" => 10
]

You may use the accept method to specify the content type that your application is expecting in response to your request.

$response = Http::accept('application/json')->get('http://example.com/users');

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS