To include headers in a cURL request in PHP, you can use the CURLOPT_HTTPHEADER
option in the curl_setopt
function. Here's an example:
// Initialize cURL
$ch = curl_init();
// Set the URL
$url = 'https://example.com/api'; // Replace with your desired URL
// Set the headers
$headers = array(
'Content-Type: application/json', // Replace with your desired headers
'Authorization: Bearer Your_Access_Token'
);
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the request
$response = curl_exec($ch);
// Close cURL
curl_close($ch);
// Handle the response
if ($response === false) {
// Request failed
echo 'Error: ' . curl_error($ch);
} else {
// Request successful
echo $response;
}
In this example, the CURLOPT_HTTPHEADER
option is set using the curl_setopt
function along with an array of headers. You can replace the example headers with the headers you need for your specific request.