从命令行调用 PHP 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13761915/
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-25 06:01:29  来源:igfitidea点击:

Call a PHP function from the command line

phpfunctioncommand-line-interface

提问by Hyman Smit

I have a file called address.php with a few functions in it. I want to call a specific function in that file from the command line. How?

我有一个名为 address.php 的文件,其中包含一些函数。我想从命令行调用该文件中的特定函数。如何?

The name of the function is called exportAddress and that function expects a single parameter.

该函数的名称称为 exportAddress,该函数需要一个参数。

回答by Tim S.

By using the -rparameter you can run a script in-line.

通过使用该-r参数,您可以在线运行脚本。

php -r "require 'address.php'; exportAddress(12345);"

php -r "require 'address.php'; exportAddress(12345);"

There are no other options. A function in PHP can only be called by a PHP script.

没有其他选择。PHP 中的函数只能由 PHP 脚本调用。

回答by user7282

Use

php  -r 'include  "/var/www/test/address.php";exportAddress(1);'

where "/var/www/test/arr.php"is the file name, including path, and exportAddress()is a function inside that file.

其中 "/var/www/test/arr.php"是文件名,包括路径,并且exportAddress()是该文件中的一个函数。

回答by Simon Rodan

Add this to the top of the file "/var/www/test/address.php"...

将此添加到文件“/var/www/test/address.php”的顶部...

foreach ($argv as $i=>$arg )
{
    if ( $arg == "exportAddress" )
    { 
        exportAddress($argv[$i+1]);
    }
}

Then from the command line, execute:

然后从命令行,执行:

php /var/www/test/address.php exportAddress 12345

回答by Samer Ata

You can make your file "somefile.php" organized as follows:

您可以按如下方式组织文件“somefile.php”:

function func1(){....}
function func2(){....}
function func3(){....}
....
foreach ($argv AS $arg){
    function_exists($arg) AND call_user_func($arg);
}

Then from the command line or a Linux cronjob, you run the following command:

然后从命令行或 Linux cronjob 运行以下命令:

php /path/to/somefile.php arg1 arg2 arg3 ...

回答by Steen Schütt

To extend on Samer Ata and Simon Rodan's answers, you can use the following to be able to execute any function with any amount of arguments:

要扩展 Samer Ata 和 Simon Rodan 的答案,您可以使用以下内容来执行具有任意数量参数的任何函数:

if(isset($argv[1]) && function_exists($argv[1])) {
  $parameters = array_slice($argv, 2);
  call_user_func($argv[1], ...$parameters);
}

This is done by removing the script and function name from $argvand then using the spread operator (...) to interpret each element in the array as an argument to call_user_func.

这是通过从 中删除脚本和函数名称$argv,然后使用展开运算符 ( ...) 将数组中的每个元素解释为 的参数来完成的call_user_func

The operator is supported from PHP 5.6 onward (and 5.5 can do some of the things using functions). You can read more about it in the official PHP documentation. For completeness, this is also known as argument unpacking.

从 PHP 5.6 开始支持运算符(并且 5.5 可以使用函数来做一些事情)。您可以在官方 PHP 文档中阅读有关它的更多信息。为完整起见,这也称为参数解包。

回答by Alobar

As of PHP 5.1.0 (7.1.0 on windows) PHP can be executed from the shell by running

从 PHP 5.1.0(Windows 上的 7.1.0)开始,PHP 可以通过运行从 shell 执行

php -a

This starts PHP's interactive shell. More info here.

这将启动 PHP 的交互式 shell。更多信息在这里

With PHP's shell, running you can require files the same way you would from a file.

使用 PHP 的 shell,运行您可以像从文件中一样需要文件。

So if the previous command was run from the folder containing the file:

因此,如果上一个命令是从包含该文件的文件夹中运行的:

php > require 'myFile.php';

If it is in a subfolder:

如果它在子文件夹中:

php > require 'path/to/file/myFile.php';

Then execute any function defined in myFile.php.

然后执行 myFile.php 中定义的任何函数。

php > myFunction(myParamsIfAny);

I guess you can use any variable defined in the same file or require any other file containing the variables needed, although I haven't tried it.

我想您可以使用在同一文件中定义的任何变量,或者需要包含所需变量的任何其他文件,尽管我还没有尝试过。