Hello Friends,
Today we will see how to make cURL HTTPs request in your laravel 8 application. This tutorial I will give you laravel 8 cURL HTTP request example.
The name stands for "Client URL". cURL is a command-line tool for getting or sending data including files using URL syntax. cURL supports HTTPS and performs SSL certificate verification by default when a secure protocol is specified such as HTTPS.
Many time you need to integrate any third party APIs in your laravel application your can done this with cURL or HTTP guzzle request. cURL is very simple and not take much more time make GET or POST HTTP APIs request.
So, let's start to implement GET and POST HTTP Request.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}
// Make Post Fields Array
$data = [
'name' => 'websolutionstuff',
'email' => '[email protected]',
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}
As a beginner, diving into a new framework like Angular 17 can seem daunting, but fear not! I'll guide you through c...
Mar-20-2024
Today in this small post i will show you how to convert excel file into json in javascript. Many time we have requi...
Jul-07-2021
Hello, laravel web developers! This article will show how to add pagination in laravel 11 Livewire. Here, we'll...
Jun-14-2024
In this tutorial we will see how to insert data into MySQL table using Node.js. In previous node .js article I will give...
Sep-29-2021