可以通过命令行将$_SERVER
变量传递给PHP脚本吗?
具体来说,我正在尝试手动设置$_SERVER['recipient']
,以便我可以在不设置邮件服务器的情况下测试电子邮件管道。
on *nix:
$ recipient="email@example.com" php script.php
<?php
print_r($_SERVER);
测试:
$ recipient="email@example.com" php script.php | grep recipient
[recipient] => something@example.com
或者,您可以导出它或设置(取决于您的操作系统),例如
$ export recipient="email@example.com"
$ setenv recipient="email@example.com"
@sberry的答案是正确的。
。但是因为我来到这个页面是为了为 $_SERVER
数组设置默认值,所以从命令行运行 PHP 时,这是我自己的答案。希望它能帮助到某人。
empty( $_SERVER['HTTP_HOST'] ) && $_SERVER['HTTP_HOST'] = 'localhost';
empty( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] = '/';
empty( $_SERVER['DOCUMENT_ROOT'] ) && $_SERVER['DOCUMENT_ROOT'] = __DIR__;
print_r( $_SERVER );
我个人使用以下方法。
示例:在我的 PHP 命令行中设置 $_SERVER['收件人]。
使用操作系统 X
- 按照 https://github.com/ersiner/osx-env-sync 中的说明进行操作
-
将以下行附加到文件"~/.bash_profile"(如果不存在,请创建它)
export recipient="something@example.com"
使用 Ubuntu GNU/Linux
-
将以下行附加到文件"/etc/environment"(创建它,如果不存在,请参阅 https://help.ubuntu.com/community/EnvironmentVariables)
recipient=something@example.com
您还可以传递两个或多个变量。例如:
$ SERVER_NAME="example.com" REQUEST_URI="?foo=bar" php script.php
<?php
print_r($_SERVER);