Shell运行/执行带有参数的php脚本

时间:2022-07-16 01:14:16

I need to execute a php file with parameters through shell.

我需要通过shell执行带有参数的php文件。

here is how I would run the php file:

下面是我如何运行php文件:

php -q htdocs/file.php

php - q根/ file.php

I need to have the parameter 'show' be passed through and

我需要传递参数“show”并

php -q htdocs/file.php?show=show_name

php - q根/ file.php ? = show_name

doesn't work

不工作

If someone could spell out to me what command to execute to get the php file to execute with set parameters, it would be much appreciated. If not, try to lead me the right direction.

如果有人能告诉我要执行什么命令才能让php文件使用set参数执行,我将非常感激。如果没有,试着引导我正确的方向。

5 个解决方案

#1


38  

test.php:

test.php:

<?php
print_r($argv);
?>

Shell:

外壳:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)

#2


4  

If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this

如果您有webserver(不仅安装了php解释器,还安装了LAMP/LNMP/等等)——请尝试一下

wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1

where:

地点:

  • « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
  • «-O -»-(字母“O”,不是0 !)将“下载的html”重定向到stdout
  • « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
  • «>/dev/null 2>&1»将stdout和stderr输出重定向到任何地方。
  • « -q » — quiet wget run
  • «-q»-安静地奔跑
  • « -t 1 » — just 1 try to connect (not like default 20)
  • «- t1»-只是尝试连接一次(不像默认的20)

In PHP's "exec" it'll be smth like this:

在PHP的“exec”中,smth是这样的:

function exec_local_url($url) {
  exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
    . addslashes($url) . '" >/dev/null 2>&1'
  );
}

// ...

exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");

So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.

因此,不需要修改脚本来处理argc/argv,可以像往常一样使用$_GET。

If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code

如果您希望在后台运行作业,请参见Unix/Windows,设置后台进程?从php代码

I use approach with wget in my cron jobs; hope it helps.

我在cron的工作中使用wget方法;希望它可以帮助。

#3


3  

You need to read command line parameters from $argc and $argv.

您需要从$argc和$argv中读取命令行参数。

Using a question mark is something you do in a URL and has nothing to do with executing PHP from a command line.

使用问号是在URL中进行的操作,与从命令行执行PHP没有任何关系。

See also: http://www.sitepoint.com/php-command-line-1/

参见:http://www.sitepoint.com/php-command-line-1/

#4


2  

In addition to the other answers (Which are quite correct), you can also pass arguments as environment parameters, like this:

除了其他答案(非常正确)之外,您还可以将参数作为环境参数传递,如下所示:

FOO=42 BAR=quux php test.php

They will then be available in the superglobal $_ENV.

它们将在superglobal $_ENV中可用。

#5


0  

If you are using it from a PHP file then you can use popen() and do something like this:

如果您是从PHP文件中使用它,那么您可以使用popen()并执行以下操作:

$part = $show_name; //or whatever you want with spaces

$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");

This uses the escapeshellarg() function in order to wrap the $part variable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.

它使用escapeshellarg()函数将$part变量包装为引号(并将其中的引号转义),以便安全地用作shell参数。

#1


38  

test.php:

test.php:

<?php
print_r($argv);
?>

Shell:

外壳:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)

#2


4  

If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this

如果您有webserver(不仅安装了php解释器,还安装了LAMP/LNMP/等等)——请尝试一下

wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1

where:

地点:

  • « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
  • «-O -»-(字母“O”,不是0 !)将“下载的html”重定向到stdout
  • « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
  • «>/dev/null 2>&1»将stdout和stderr输出重定向到任何地方。
  • « -q » — quiet wget run
  • «-q»-安静地奔跑
  • « -t 1 » — just 1 try to connect (not like default 20)
  • «- t1»-只是尝试连接一次(不像默认的20)

In PHP's "exec" it'll be smth like this:

在PHP的“exec”中,smth是这样的:

function exec_local_url($url) {
  exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
    . addslashes($url) . '" >/dev/null 2>&1'
  );
}

// ...

exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");

So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.

因此,不需要修改脚本来处理argc/argv,可以像往常一样使用$_GET。

If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code

如果您希望在后台运行作业,请参见Unix/Windows,设置后台进程?从php代码

I use approach with wget in my cron jobs; hope it helps.

我在cron的工作中使用wget方法;希望它可以帮助。

#3


3  

You need to read command line parameters from $argc and $argv.

您需要从$argc和$argv中读取命令行参数。

Using a question mark is something you do in a URL and has nothing to do with executing PHP from a command line.

使用问号是在URL中进行的操作,与从命令行执行PHP没有任何关系。

See also: http://www.sitepoint.com/php-command-line-1/

参见:http://www.sitepoint.com/php-command-line-1/

#4


2  

In addition to the other answers (Which are quite correct), you can also pass arguments as environment parameters, like this:

除了其他答案(非常正确)之外,您还可以将参数作为环境参数传递,如下所示:

FOO=42 BAR=quux php test.php

They will then be available in the superglobal $_ENV.

它们将在superglobal $_ENV中可用。

#5


0  

If you are using it from a PHP file then you can use popen() and do something like this:

如果您是从PHP文件中使用它,那么您可以使用popen()并执行以下操作:

$part = $show_name; //or whatever you want with spaces

$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");

This uses the escapeshellarg() function in order to wrap the $part variable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.

它使用escapeshellarg()函数将$part变量包装为引号(并将其中的引号转义),以便安全地用作shell参数。