从命令行运行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10262532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:46:07  来源:igfitidea点击:

Running PHP script from the command line

phpcommand-line

提问by Siddharth

How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?

如何使用用于解析 Web 脚本的 PHP 解释器从命令行运行 PHP 脚本?

I have a phpinfo.phpfile which is accessed from the web shows that Germanis installed. However, if I run the phpinfo.phpfrom the command line using - php phpinfo.phpand grepfor German, I don't find it. So both phps are different. I need to run a script which the phpon which Germanis installed.

我有一个phpinfo.php从 web 访问的文件显示German已安装。但是,如果我phpinfo.php使用 -php phpinfo.phpgrepfor从命令行运行,则German找不到它。所以两个phps是不同的。我需要运行该脚本phpGerman安装。

How can I do this?

我怎样才能做到这一点?

回答by Sampo Sarrala - codidact.org

UPDATE:

更新:

After misunderstanding, I finally got what you are trying to do. You should check your server configuration files; are you using apache2 or some other server software?

经过误会,我终于明白了你想要做什么。你应该检查你的服务器配置文件;您使用的是 apache2 还是其他一些服务器软件?

Look for lines that start with LoadModule php... There probably are configuration files/directories named modsor something like that, start from there.

寻找以LoadModule php...开头的行,可能有命名的配置文件/目录mods或类似的东西,从那里开始。

You could also check output from php -r 'phpinfo();' | grep phpand compare lines to phpinfo();from web server.

您还可以检查来自 Web 服务器的输出php -r 'phpinfo();' | grep php并将行与phpinfo();来自 Web 服务器的行进行比较。

To run phpinteractively:

php交互方式运行:

(so you can paste/write code in the console)

(因此您可以在控制台中粘贴/编写代码)

php -a

To make it parse file and output to console:

要使其解析文件并输出到控制台:

php -f file.php

Parse file and output to another file:

解析文件并输出到另一个文件:

php -f file.php > results.html

Do you need something else?

你需要别的东西吗?

To run only small part, one line or like, you can use:

要仅运行一小部分、一行或类似内容,您可以使用:

php -r '$x = "Hello World"; echo "$x\n";'

If you are running linux then do man phpat console.

如果您正在运行 linux,则man php在控制台执行。

if you need/want to run php through fpm, use cli fcgi

如果您需要/想要通过 fpm 运行 php,请使用 cli fcgi

SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"

where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

其中 /var/run/php-fpm/php-fpm.sock 是您的 php-fpm 套接字文件。

回答by Leandro

I was looking for a resolution to this issue in Windows, and it seems to be that if you don't have the environments vars ok, you need to put the complete directory. For eg. with a file in the same directory than PHP:

我一直在寻找 Windows 中这个问题的解决方案,似乎是如果你没有环境变量,你需要放置完整的目录。例如。与 PHP 位于同一目录中的文件:

F:\myfolder\php\php.exe -f F:\myfolder\php\script.php

回答by Shannon White

On SuSE, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (php configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.

在 SuSE 上,PHP 有两种不同的配置文件:一种用于 Apache,一种用于 CLI(命令行界面)。在 /etc/php5/ 目录中,您将找到一个“apache2”目录和一个“cli”目录。每个都有一个“php.ini”文件。这些文件的用途相同(php 配置),但适用于运行 PHP 的两种不同方式。除其他外,这些文件加载​​ PHP 使用的模块。

If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the gearman module, while the cli php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.

如果您的操作系统相似,那么这两个文件可能不一样。您的 Apache php.ini 可能正在加载 gearman 模块,而 cli php.ini 则没有。当模块安装(自动或手动)时,它可能只更新了 Apache php.ini 文件。

You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.

您可以简单地将 Apache php.ini 文件复制到 cli 目录中,以使 CLI 环境与 Apache 环境完全相同。

Or, you could find the line that loads the gearman module in the Apache file and copy/paste just it to the CLI file.

或者,您可以在 Apache 文件中找到加载 gearman 模块的行,然后将其复制/粘贴到 CLI 文件中。