search

Home  >  Q&A  >  body text

How to use environment variables on Windows/Linux..?

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粉921130067P粉921130067330 days ago485

reply all(1)I'll reply

  • P粉704196697

    P粉7041966972024-01-07 09:34:58

    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.

    reply
    0
  • Cancelreply