不同域上的外部 API 和临时服务器

发布于 2024-09-06 02:30:27 字数 93 浏览 2 评论 0原文

我的临时服务器和生产服务器位于不同的服务器和域上。

处理具有依赖于域名的密钥的外部 API 的最佳方式是什么? 这是不好的做法吗?两者应该位于同一服务器上?

My staging and 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-09-13 02:30:27

好吧,我自己解决这个问题的方法是在数组中针对不同的环境使用不同的键。

在这种情况下,我将尝试用 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
    }
}

希望这有帮助......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文