Laravel 8 cURL HTTP Request Example

Laravel 8 cURL HTTP Request Example

Hello friends, Today we will show Laravel 8 cURL HTTP Request Example Tutorial. In this example we will show how to make cURL HTTPs request in your laravel 8. We will provide example of laravel 8 http request get parameters. In this post we will discus on how to call curl get request in laravel 8 application.

Here we will explain http curl post request example. Also we will provide example of laravel curl post request with headers. we will show laravel curl post request with headers example.

Read Also :  Laravel 8 Yajra Datatable Example Tutorial

Laravel 8 Http cURL GET Request Example

$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));
}

Laravel 8 Http cURL POST Request Example

// Make Post Fields Array
$data = [
    'name' => 'websolutionstuff',
    'email' => 'websolutionstuff@gmail.com',
];

$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));
}

I hope you understand of Laravel 8 cURL HTTP Request Tutorial and it can help you…

Read Also :  Laravel Carbon Get Year from Date Example