Jac*_*mit 34 php function command-line-interface
我有一个名为address.php的文件,其中包含一些函数.我想从命令行调用该文件中的特定函数,怎么样?该函数的名称称为exportAddress,该函数需要一个参数
Tim*_* S. 68
通过使用该-r参数,您可以在线运行脚本.
php -r "require 'address.php'; exportAddress(12345);"
没有其他选择.PHP中的函数只能由PHP脚本调用.
小智 6
将其添加到文件“/var/www/test/address.php”的顶部...
foreach ($argv as $i=>$arg )
{
if ( $arg == "exportAddress" )
{
exportAddress($argv[$i+1]);
}
}
Run Code Online (Sandbox Code Playgroud)
然后从命令行执行:
php /var/www/test/address.php exportAddress 12345
Run Code Online (Sandbox Code Playgroud)
用
php -r 'include "/var/www/test/address.php";exportAddress(1);'
Run Code Online (Sandbox Code Playgroud)
其中 "/var/www/test/arr.php"是文件名,包括路径,并且exportAddress()是该文件中的一个函数。
您可以按如下方式组织文件“somefile.php”:
function func1(){....}
function func2(){....}
function func3(){....}
....
foreach ($argv AS $arg){
function_exists($arg) AND call_user_func($arg);
}
Run Code Online (Sandbox Code Playgroud)
然后从命令行或 Linux cronjob 运行以下命令:
php /path/to/somefile.php arg1 arg2 arg3 ...
Run Code Online (Sandbox Code Playgroud)