How to use environment variables on Windows/Linux..?
P粉921130067
P粉921130067 2024-01-06 14:02:44
0
1
480

I'm trying to use the OpenAI library (https://github.com/orhanerday/open-ai) which uses environment variables for key storage, but when I run it it doesn't seem to be able to find the key.

On my local Windows machine, I ran the following command: setx OPENAI_API_KEY "mykey"

On the Linux web server, I ran the following command: export OPENAI_API_KEY=mykey

Now when I run the following command on the server I see the correct key value printed back to me: printenv OPENAI_API_KEY

In my script I use $open_ai_key = getenv('OPENAI_API_KEY'); but I don't get any value..??

Any information on how to resolve this issue would be greatly appreciated. Thanks!

P粉921130067
P粉921130067

reply all(1)
P粉704196697

Thank you for using orhanerday/OpenAI PHP SDK,

Let’s try setting the “OPENAI_API_KEY” environment variable through the control panel

  1. Open System Properties and select Advanced System Settings

  2. Select environment variables...

  3. Select "New..." from the "User Variables" section (top). Add your name/key-value pair, replacing it with your API key.

Variable name: OPENAI_API_KEY
Variable value: <yourkey>
  1. Log out and log in to your computer.
  1. Create PHP file;
<?php
    $open_ai_key = getenv("OPENAI_API_KEY");
    print("OPENAI_API_KEY is; $open_ai_key");
  1. Run PHP file
$ php index.php 
> OPENAI_API_KEY is: sk-gjtv.....

After running the application, you should get the value.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template