Laravel 8 cURL HTTP Request Example

Websolutionstuff | Apr-28-2021 | Categories : Laravel

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.

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

 

 

POST Request Example :

 

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

 

Recommended Post
Featured Post
How To Toggle Dark and Light Mode using jQuery
How To Toggle Dark and Light M...

In this article, we will see how to toggle between dark and light modes using jquery. As per the current trend of web de...

Read More

Nov-24-2020

Laravel 8 One To Many Polymorphic Relationship
Laravel 8 One To Many Polymorp...

In this tutorial we will learn about laravel 8 one to many polymorphic relationship. A one-to-many polymorphic rela...

Read More

Nov-19-2021

Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Exa...

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...

Read More

Nov-24-2021

Carbon Add Minutes To Date In Laravel 9
Carbon Add Minutes To Date In...

In this article, we'll explore how to add minutes to a date in Laravel 8, Laravel 9 and Laravel 10 using Carbon...

Read More

Nov-23-2022