我的登台和生产服务器位于不同的服务器和域上。
处理具有依赖于域名的密钥的外部 API 的最佳方法是什么?这是不好的做法,两者都应该在同一台服务器上吗?
好吧,我自己对这个问题的解决方案是在不同的环境中使用数组中的不同键。
在这种情况下,我将尝试用 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
}
}
我希望这有帮助...