3

I'm trying to use SendGrid's API for which I need to access an environment variable that I've added to my root directory using the following command.

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

This has created a sendgrid.env file in my root folder, added sendgrid.env to my .gitignore file, and added SENDGRID_API_KEY as an environment variable.

However, PHP's getenv('SENDGRID_API_KEY') key is not returning anything, and PHP's phpinfo() does not reflect SENDGRID_API_KEY as an environment variable.

Here are the API installation instructions.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • Why do you think PHP should read your `sendgrid.env` file? Have you tried to use `putenv()`? – Ruslan Osmanov Nov 10 '16 at 04:11
  • Hi Ruslan. I don't think it should and I know it's not. However, I think it should be able to read the environment variable, hence the `source ./sendgrid.env` command. I am just following the API instructions and they don't say anything about putenv. I've added a link to the question. The installation is fairly short and straightforward except for this little piece. – Govind Rai Nov 10 '16 at 04:16

1 Answers1

1

It is implied that you should use another package for reading the .env files. There is a sample on their official site that uses a Dotenv class to load contents of the files into environment:

<?php

require 'vendor/autoload.php';
Dotenv::load(__DIR__);
$sendgrid_apikey = getenv('YOUR_SENDGRID_APIKEY');

Still, they don't even explain where the class comes from. Apparently, they mean that you should install vlucas/phpdotenv package. Note, that in the current version of this package, the load method is non-static (it actually was static in early versions):

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • *_* i'm pretty beginner. i would've never figured that out.... Do you have any other recommendation to solve this without referencing the package? And if at the end of the day, you're reading the .env file, what is the point of running `source ./sendgrid.env`. I super appreciate your assistance. +1 (and soon to be marked as answer). – Govind Rai Nov 10 '16 at 04:35
  • 1
    I think you don't have to use environment variables at all, unless you find them convenient. Constructor accepts a string, and it's up to you how to fetch that key. I'm used to use configuration files included into `.gitignore`. Another way is to make a shell wrapper like `YOUR_SENDGRID_KEY='123abcdef...' php yourscript-calling-getenv.php`, if it is a CLI script. If the script is supposed to run in another SAPI (such as FPM or apache), you can tweak the webserver to pass specific env. variables to all PHP script, or to PHP scripts within specific directory. It all depends on the specific task – Ruslan Osmanov Nov 10 '16 at 04:44
  • 2
    Thanks Ruslan. Super helpful. Funny enough this is right out of phpdotenv's readme "**phpdotenv is made for development environments, and generally should not be used in production. In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request.**". I guess, since I am handling production code solely, I could've just gone ahead with the real key, but this is a valuable lesson on how to better securely share code when collaborating. – Govind Rai Nov 10 '16 at 08:05