This article provides a brief tutorial on how to set the authorization header to a cURL request in PHP.

The article covers the basics of setting up a cURL request, including how to set the custom header to a cURL request.
CONTENTS
Send using the cURL basic authentication method:
If you’re using PHP to access a resource that requires HTTP authentication, you can use the CURLOPT_HTTPAUTH and CURLOPT_USERPWD options to set the username and password.
<?php
$user_name = "foo";
$password = "bar"
//initiate the cURL
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://codehasbug.com',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$user_name:$password"
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Send using the cURL custom header method:
You can also authenticate using the cURL custom header. Let’s check the below code:
<?php
$publicKey = "abcd1234";
$secKey = "efgh5678"
//initiate the cURL
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://codehasbug.com',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic " . base64_encode($publicKey . ':' . $secKey)
),
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Final Words:
I think the above methods will help you to set the authorization header to a cURL request. That’s all for today’s tutorial. You can also learn something new in PHP from our PHP Tutorial. If you have any questions please comment below.
I think the above methods will help you to set the authorization header to a cURL request. That’s all for today’s tutorial. You can also learn something new in PHP If you have any questions please comment below.
https://www.clickercounter.org/