Skip to content
PHP Logo

How can environment variables be set in PHP to access Google API credentials?

To set environment variables in PHP to access Google API credentials, you can use the `putenv()` function to set the variables before making the API call. This allows you to securely store sensitive information like API keys and credentials outside of your codebase.

php
// Set Google API credentials as environment variables
putenv('GOOGLE_API_KEY=your_api_key_here');
putenv('GOOGLE_CLIENT_ID=your_client_id_here');
putenv('GOOGLE_CLIENT_SECRET=your_client_secret_here');

// Access the environment variables
$apiKey = getenv('GOOGLE_API_KEY');
$clientID = getenv('GOOGLE_CLIENT_ID');
$clientSecret = getenv('GOOGLE_CLIENT_SECRET');

// Make API call using the credentials
// Example:
// $client = new Google_Client();
// $client->setDeveloperKey($apiKey);
// $client->setClientId($clientID);
// $client->setClientSecret($clientSecret);