API esterne e server di gestione temporanea su un dominio diverso
Domanda
I miei di sosta e di produzione server sono su diversi server e domini.
Quale sarebbe il modo migliore per affrontare le API esterne che hanno una chiave che si basa sui nomi a dominio? È questa cattiva pratica ed entrambi dovrebbero essere sullo stesso server?
Soluzione
Bene, la mia soluzione a questo problema sta usando diverse chiavi di un array per ambienti diversi.
In questo caso cercherò di spiegarlo 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
}
}
Spero che questo aiuta ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow