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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵