External APIs and staging server on different domain
My staging an开发者_如何学God production servers are on different servers and domains.
Which would be the best way to deal with external APIs that have a key that relies on domain names? Is this bad practice and both should be on the same server?
Well, my own solution to this problem is using different keys in an array for different environments.
In this case i'll try to explain it in PHP
class API_Client
{
const ENV_STAGING = 'staging';
const ENV_PRODUCTION = 'production';
protected static $apiKeys = array(
self::ENV_STAGING => 'thisisthekeyformystagingenv',
self::ENV_PRODUCTION => 'thisisthekeyformyproductionenv',
);
protected static $environment = self::ENV_PRODUCTION;
public static function getEnvironment()
{
return self::$environment;
}
public static function setEnvironment($environment)
{
self::$environment = $environment;
}
public static function apiCall($call)
{
$environment = self::getEnvironment();
if(array_key_exists(self::$apiKeys, $environment))
$apiKey = self::$apiKeys[$environment];
else throw new Exception("No API key found for current environment '$environment'");
return self::_apiCall($apiKey, $call);
}
protected static function _apiCall($apiKey, $call)
{
// Make the call to the API
}
}
I hope this helps...
精彩评论