php - 我会将我的 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'
];
`
解决方案
让我们用下面这行代码来分析凭证的存储位置
'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'
];
推荐阅读
- hexo - 如何在 Hexo 博客中添加 RSS 提要
- watson-studio - ImportError:Watson Studio 上没有名为“cv2”的模块
- android - 不可触摸的搜索栏但显示进度
- intellij-idea - 升级后无法启动 IntelliJ IDEA
- c - C 不兼容的类型结构返回
- python - 如何从python中的特定列中读取值?
- powershell - 未找到调用 sql cmdlet/batchparser.dll
- kendo-asp.net-mvc - Child DropDownListFor 在级联成功后不显示过滤数据
- php - 无法从动态多维数组中获取数据
- python - 如何仅使用 c/python api 将 c++ 成员函数绑定到 python?