从命令行调用PHP函数(Call a php function from the command l

2019-07-04 17:28发布

我有一个名为address.php在它的一些功能文件。 我想在命令行中,如何调用该文件中的特定功能? 该函数的名称叫做exportAddress和函数需要一个参数

Answer 1:

通过使用-r参数,您可以运行在嵌入式脚本。

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

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



Answer 2:

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

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

然后从执行命令行#> PHP /var/www/test/address.php exportAddress 12345



Answer 3:

PHP -R '包括“/var/www/test/address.php";exportAddress(1);'

其中"/var/www/test/arr.php"是文件名包括路径和exportAddress()是文件中的功能



Answer 4:

你可以让你的文件“somefile.php”安排如下:

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

然后,在命令行或Linux的cronjob,运行以下命令

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


文章来源: Call a php function from the command line