首页 > 解决方案 > 我会将我的 API 密钥放在这个脚本中的什么位置?

问题描述

我一般是编码新手。但是以前从未使用过PHP。把我的钥匙放在哪里还不是很清楚。

更多上下文:https ://github.com/erikaheidi/dynacover

`

<?php

return [
    //Twitter API Keys
    'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY',
    'twitter_consumer_secret' => getenv('TW_CONSUMER_SECRET') ?: 'APP_CONSUMER_SECRET',
    'twitter_user_token' => getenv('TW_USER_TOKEN') ?: 'USER_ACCESS_TOKEN',
    'twitter_token_secret' => getenv('TW_USER_TOKEN_SECRET') ?: 'USER_ACCESS_TOKEN_SECRET',

    //GitHub Personal Token (for templates using GH Sponsors)
    'github_api_bearer' => getenv('GITHUB_TOKEN') ?: 'GITHUB_API_BEARER_TOKEN',

    //Default Template
    '`default_template`' => getenv('DEFAULT_TEMPLATE') ?: 'app/Resources/templates/cover_basic.json'
];

`

标签: php

解决方案


让我们用下面这行代码来分析凭证的存储位置

'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY'
  • PHP会首先使用getenv()获取环境变量TW_CONSUMER_KEY
  • 如果找不到环境变量,它使用常量字符串"APP_CONSUMER_KEY"——这对你的情况可能不是很有帮助

已建立凭据旨在存储在示例中的环境变量中,也许此答案将帮助您进行下一步:如何为 PHP 设置全局环境变量

现在,您可以使用以下代码设置变量来测试凭据是否有效:

putenv('TW_CONSUMER_KEY=your_key_here');
putenv('TW_CONSUMER_SECRET=your_secret_key');
// And so on, and so forth :)

return [
    //Twitter API Keys
    'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY',
    'twitter_consumer_secret' => getenv('TW_CONSUMER_SECRET') ?: 'APP_CONSUMER_SECRET',
    'twitter_user_token' => getenv('TW_USER_TOKEN') ?: 'USER_ACCESS_TOKEN',
    'twitter_token_secret' => getenv('TW_USER_TOKEN_SECRET') ?: 'USER_ACCESS_TOKEN_SECRET',

    //GitHub Personal Token (for templates using GH Sponsors)
    'github_api_bearer' => getenv('GITHUB_TOKEN') ?: 'GITHUB_API_BEARER_TOKEN',

    //Default Template
    '`default_template`' => getenv('DEFAULT_TEMPLATE') ?: 'app/Resources/templates/cover_basic.json'
];

推荐阅读