How to work with environment variables on Windows/Linux..? - php

I am trying to work with an OpenAI library (https://github.com/orhanerday/open-ai) that uses environment variables for key storage, but it doesn't seem to be finding the key when I run it.
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 on the server when I run the following, I see the correct key value printed back to me: printenv OPENAI_API_KEY
In my script I'm using $open_ai_key = getenv('OPENAI_API_KEY'); but I'm getting no value back..??
Any information on how I can resolve this would be greatly appreciated. Thanks!

Thank you for using orhanerday/OpenAI PHP SDK,
Let's try to set your ‘OPENAI_API_KEY’ Environment Variable through the Control Panel
Open System properties and select Advanced system settings
Select Environment Variables...
Select New… from the User variables section(top). Add your name/key-value pair, replacing with your API key.
Variable name: OPENAI_API_KEY
Variable value: <yourkey>
Sign out and then log in to your PC.
Create a PHP file;
<?php
$open_ai_key = getenv("OPENAI_API_KEY");
print("OPENAI_API_KEY is; $open_ai_key");
run the PHP file
$ php index.php
> OPENAI_API_KEY is: sk-gjtv.....
After running the app you should get the value.

Related

not able to execute nutch crawl command using php exec function

I have to run Nutch crawl commands using php exec but it shows
"0 Error: JAVA_HOME is not set"
The command works fine with terminal. I have tried the below code in crawl.php where apache-nutch-1.15 directory is placed.
exec('apache-nutch-1.15/bin/nutch inject crawl/crawldb urls',$output);
and this gives the above mentioned error.
Thank you in advance for any assistance you can provide.
In order to run Nutch you need the JAVA_HOME environment variable set and pointing to the proper path (where your JVM is installed). This works on your terminal because you have this variable set already. You can check this with:
$ env | grep JAVA
When a new process is started with exec from PHP, this environment variable is not set because it is not a shell, you're only starting a process without any "shell environment". You can use the putenv function to specify some environment variables before calling exec.

Add environment variable to access from php script without reboot?

I need to access an environment variable in my php script. It's running on a remote server. I've added them to /etc/environment but I can't access them as the server needs a reboot.
They can be echoed when I run
source /etc/environment
so it's not a problem with the file.
But the php script isn't running as my user so it doesn't have those variables. Since it's a remote server I don't particularly want to reboot it, as I assume this would then pull in the new environment variables I wrote to /etc/environment.
I've seen I can use /etc/profile but that won't affect the apache user, which I assume is running the php scripts.
I've tried to run
echo exec('source /env/environment && echo $the_var');
but the variable still doesn't return.
Any ideas please?
Thanks in advance

Retrieving an environment variable in php

I am using a Linux EC2 instance on Amazon Web Services where I set up the instance-id as an environment variable by executing
export EC2_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
When I try to retrieve the data from the variable in php using $_ENV['EC2_INSTANCE_ID'] or getenv("EC2_INSTANCE_ID") I get an error:
Undefined index: EC2_INSTANCE_ID
(This error comes from $_ENV['...'], getenv() simply does not return the value of the variable.)
I already set up environment variables in the php.ini by setting variables_order = "EGPCS".
When I run printenv in the command line I get EC2_INSTANCE_ID=i-039......
I restarted apache by using service httpd restart after setting the environment variable.
So why is php not fetching the variable?
If you execute
export VARNAME=value
in the shell, as you suggest in your question, then a PHP program running in a web server will not be able to read it.
If you use Apache for example, then you will need to set that variable in the Apache configuration for that particular virtual host.. something like:
<VirtualHost ser.ver:80>
SetEnv VARNAME value
</VirtualHost>
Then, a PHP program on that server will be able to read that variable with getenv() or $_ENV as you expect.
What you describe would only work if your PHP program is executed as client program in your computer:
$ export VARNAME=value
$ cat "<?php echo getenv('VARNAME'); ?>" > test.php
$ php test.php

How to use PHP to access MySQL shell?

Currently I have a shell script within my application that depending on what argument you pass it, it will log you into either the live or development MySQL database shell. It works fine, no problem there.
The issue/challenge I am having is that if in the case my MySQL credentials (host / port) change for either the live or development database then I will manually have to edit this shell script, updating it will the new arguments. Problem is, I have done this before but I never want to have to do it again.
What I would like to do is have a PHP script (mysql.sh.php) that when executed, depending on the argument passed to it will log you into either the live or development database shell. How it will differ from the shell script is that it will pull the current credentials (and even host and port) from a PHP configuration class and pass those as arguments to a shell command that will log into the respective database.
Below gives you an illustration of what I am attempting.
#!/usr/bin/php
<?php
include ('common.php');
//Pull info from PHP class right here
exec("mysql --host={$myhostnotyours} --user={$myusernotyours} -p{$mypassnotyours} {$thedatabase}");
What I expect or would like is
mysl>
However, the command just hangs and I am not presented with the MySQL shell.
I would like to know if there is a way to accomplish what I am trying.
Thanks!

How to get windows current login details

I have tried to get the current login name for user. i'm using windows 8.
When i execute the command as "whoami" then i'm getting the correct login name.,
When i execute the same in exec("whoami"); then i will get the user account that have created on this system.
Any idea why its happen like that .
i'm using PHPDesktop application. Even when i run the BAT in cmd then its return correct value. But same BAT returns user account name when its run in PHP.
Even tried get_current_user()., $_ENV , $_SERVER ,
No luck.
The user account name (= login name) is held in environment variable USERNAME on Windows. So you just have to get the value of this environment variable.
Open a command prompt window, just enter set and look on the list of environment variables output. You see now all standard environment variables on Windows like APPDATA, USERNAME, USERPROFILE, etc.
See Wikipedia article about Windows Environment Variables for a list of predefined environment variables with description.

Categories