Laravel 8 HTTP cURL POST Request With Headers Example
Teams applying “Laravel 8 HTTP cURL POST Request With Headers Example” in a production project can use details on Monitask 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 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
]
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial