dsjklb0205 2019-07-06 16:33
浏览 116
已采纳

使用PHP脚本获取Heroku环境变量

I can't access my Heroku environment variables when I run a PHP script. On the picture below you can see that I have variable TEST_VAR.

create heroku env var

The code snippet below should output this variable, I get nothing instead:

$url = getenv('TEST_VAR');
echo $url;

I tried this and got 'Error!', so getenv('TEST_VAR') is false:

if (getenv('TEST_VAR')) {
    $url = getenv('TEST_VAR');
    echo $url;
} else {
    die('Error!');
}

In this picture you can see the output of both snippets (empty string and Error!).

output

  • 写回答

1条回答 默认 最新

  • drlma06060 2019-07-07 14:23
    关注

    It looks like you're running that code on your local command line. heroku config:set sets environment variables for your app on Heroku. It doesn't do anything to your local environment.

    To use environment variables locally you must set them another way. You could do it manually, using whatever tools your operating system provides, but the Heroku CLI also provides a heroku local command that conveniently reads environment variables from a file called .env and populates them for you.

    Simply create an .env file containing your variables, e.g.

    TEST_VAR=hello_world
    

    and then run your code via heroku local. By default this will run your web process as defined in your Procfile. If you have already set up variables using heroku config:set you can copy them to your .env file:

    heroku config:get TEST_VAR >> .env
    

    Note that the .env file should not be committed to your repository. It's meant for local use only. On Heroku, you should continue to set config vars using heroku config:set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题