Setting PHP Enviromental Variable While Running Command Line Script

How to pass environment variables to a php script in windows

I finally got this working.

On Unix systems you can set the environment variables on the same line invoking the script.

QUEUE=notification VVERBOSE=1 php resque.php

But for windows Os, you need to first set the environment variable like this

SET QUEUE=notification

SET VVERBOSE=1

Then you invoke the script

php resque.php

Thank you to all who contributed...!

Pass a variable to a PHP script running from the command line

The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.

You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).

If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:

#!/bin/sh
wget http://location.to/myfile.php?type=daily

Or check in the PHP file whether it's called from the command line or not:

if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}

(Note: You'll probably need/want to check if $argv actually contains enough variables and such)

How to properly set PHP environment variable to run commands in Git Bash

Adding the path to your PATH variable should fix that.

Right click My Computer, go to advanced settings, click Environment Variables then edit the PATH system variable.

Add a semi-colon and then the path to your PHP binary, i.e. ";C:\wamp\bin\php\php5.3.8"

Finally, restart the Git Bash so that it updates the PATH variable.

PHP - Run script with environment variables

This function: http://php.net/proc_open has 5th argument named $env to pass any environment variables to the new process.

This is the shortest code I used to run it:

$proc = proc_open(
"php demo-2.php", // put your python script here
[
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'a'],
],
$pipes,
null,
[
'SECRET' => $secret,
]
);
print stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($proc);

BTW my demo-2.php only contains <?php print_r($_ENV); ?>.

PS. In case you try it with PHP and it only prints empty array, edit your php.ini and set variables_order to contain E, which is responsible for populating $_ENV variable.

Detecting the environment in PHP CLI scripts

You can use php_uname('n') to get the hostname of your current machine. From there it's up to your implementation to determine whether it's production, staging, or development, either based on patterns in host name, hard-coded values, or some other configuration file.

Passing an environment-variable into a php commandline script

Don't use $ in the export command, it should be:

export APP_ENV

You can combine this with the assignment:

export APP_ENV="development"

With the $, you were effectively doing:

export development

How to access PHP with the Command Line on Windows?

You need to add your PHP installation directory to the %PATH% environment variable, or work from the PHP installation directory.

To add it to path (The best approach - Edited for Windows 7):

  • Right-click on a My Computer icon
  • Click Properties
  • Click Advanced system settings from the left nav
  • Click Advanced tab
  • Click Environment Variables button
  • In the System Variables section, select Path (case-insensitive) and click Edit button
  • Add a semi-colon (;) to the end of the string, then add the full file system path of your PHP installation (e.g. C:\Program Files\PHP)
  • Keep clicking OK etc until all dialog boxes have disappeared
  • Close your command prompt and open it again
  • Sorted

Alternatively, you can run cd <PHP installation path> before you try and run you command, or call your script like <FULL file system path of php.exe> <path to script>



Related Topics



Leave a reply



Submit