PHP cURL Request with Bearer Token Authorization Example

Sure, here’s an example of making a cURL request with Bearer token authorization header using PHP:

<?php
// API endpoint URL
$apiUrl = 'https://example.com/api/endpoint';

// Your Bearer token
$token = 'your_bearer_token';

// Create cURL resource
$ch = curl_init($apiUrl);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $token
));

// Execute the cURL request
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
}

// Close cURL resource
curl_close($ch);

// Process the response
if ($response) {
    // Do something with the API response
    echo $response;
} else {
    // Handle the case where the request failed
    echo 'Request failed';
}
?>

Replace 'https://example.com/api/endpoint' with the actual API endpoint URL you want to call. Also, replace 'your_bearer_token' with the valid Bearer token required for authorization with the API. The example sets the Bearer token in the request header using Authorization: Bearer header. Remember to use HTTPS for secure communication with the API to protect sensitive information.