Today, we are share with you how to make cURL HTTPs request in your laravel application. in many time you need to integrate any third party APIs in your laravel application. your can done this with cURL or HTTP guzzle request. but cURL is so simple and not take much time make get or post HTTP APIs request.
Make GET 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 Request
// Make Post Fields Array
$data1 = [
'data1' => 'value_1',
'data2' => 'value_2',
];
$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($data2),
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));
}
We are hope this tutorials is helpfull to everyone..
[ADDCODE]
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Firebase Authentication with Anguler 9
In this Angular tutorial, we are going t...jQuery get Value of Selected Radio Button
jQuery radio button is used when you wan...Laravel7 and Angular Token-Based Authentication using JWT
Implement Laravel Authentication JSON We...Angular 12 Copy to Clipboard Example Tutorial
There are so many JavaScript plugin avai...How to remove the first element from an array in PHP
Use the PHP array_shift() func...