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 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决