Laravel 5.5 – How To Make cURL HTTP Request Example
In this post we will give you information about Laravel 5.5 – How To Make cURL HTTP Request Example. Hear we will give you detail about Laravel 5.5 – How To Make cURL HTTP Request ExampleAnd how to use it also give you demo for it if it is necessary.
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]
Hope this code and post will helped you for implement Laravel 5.5 – How To Make cURL HTTP Request Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs