In this article, we will see how to make cURL HTTP requests in laravel 8. In this tutorial, I will give you laravel 8 cURL HTTP request example. The name stands for "Client URL". A 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.
So, let's see cURL HTTP request, cURL example, cURL get request, cURL in laravel 8, cURL API, cURL post request, cURL get request with parameters, cURL get request PHP.
Many times you need to integrate any third-party APIs in your laravel application you can do this with a cURL or HTTP guzzle request. cURL is very simple and does not take much more time to make GET or POST HTTP APIs requests.
In this example, we initialize the cURL and use the GET data from the URL.
$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));
}
In post request, we need to pass the parameter in the CURLOPT_POSTFIELDS field.
// Make Post Fields Array
$data = [
'name' => 'techsolutionstuff',
'email' => 'test@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));
}
You might also like :