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 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错