What best practices should be followed when handling login credentials and sensitive data in PHP scripts for external API calls?
When handling login credentials and sensitive data in PHP scripts for external API calls, it is crucial to follow best practices to ensure the security of the data. One common approach is to store sensitive data such as API keys and passwords in environment variables or configuration files outside of the web root directory. This helps prevent unauthorized access to the data in case of a security breach.
// Example of storing sensitive data in environment variables
$api_key = getenv('API_KEY');
$api_secret = getenv('API_SECRET');
// Example of using stored credentials in API call
$api_url = 'https://api.example.com';
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $api_key,
'Secret: ' . $api_secret
));
$response = curl_exec($ch);
curl_close($ch);