将变量传递给从命令行运行的php脚本

时间:2021-07-09 23:53:14

I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:

我有一个PHP文件需要从命令行(通过crontab)运行。我需要将type=daily传递给文件,但我不知道如何传递。我试着:

php myfile.php?type=daily

but this error was returned:

但是这个错误被返回:

Could not open input file: myfile.php?type=daily

无法打开输入文件:myfile.php?type=daily ?

What can I do?

我能做什么?

9 个解决方案

#1


97  

The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.

?type=daily参数(以$_GET数组结尾)仅对web访问页面有效。

You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).

您需要像调用myphp文件一样调用它。每日php并从$argv数组中检索该参数(它将是$argv[1],因为$argv[0]将是myfile.php)。

If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:

如果该页面也被用作网页,那么您可以考虑两个选项。或者使用shell脚本访问它,然后从cron调用wget:

#!/bin/sh
wget http://location.to/myfile.php?type=daily

Or check in the php file whether it's called from the commandline or not:

或者在php文件中检查它是否从命令行调用:

if (defined('STDIN')) {
  $type = $argv[1];
} else { 
  $type = $_GET['type'];
}

(Note: You'll probably need/want to check if $argv actually contains enough variables and such)

(注意:您可能需要/想要检查$argv是否包含足够的变量等)

#2


47  

Just pass it as normal parameters and access it in PHP using the $argv array.

只需将它作为普通参数传递,并使用$argv数组在PHP中访问它。

php myfile.php daily

and in myfile.php

而在myfile.php

$type = $argv[1];

#3


9  

These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:

这些行将转换CLI调用(如php myfile)的参数。php“type=daily&foo=bar”进入著名的$ _get数组:

if (!empty($argv[1])) {
  parse_str($argv[1], $_GET);
}

Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.

虽然覆盖全局$ _get数组是相当麻烦的,但是它可以快速地将所有脚本转换为接受CLI参数。

See http://php.net/manual/en/function.parse-str.php for details.

有关详细信息,请参阅http://php.net/manual/en/function.parse-str.php。

#4


6  

parameters send by index like other application

与其他应用程序一样,参数按索引发送

php myfile.php type=daily

and then you can gat them like this

然后你可以像这样做

<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
    echo $arg;
?>

#5


4  

Save this code in file myfile.php and run as php myfile.php type=daily

将此代码保存到文件myfile中。php和run作为php myfile。php类型=每天

<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg){
        if ($key > 0){
            list($x,$y) = explode('=', $arg);
            $b["$x"]    = $y;  
           }
       }
?>

If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

如果您添加var_dump($ b);在?>标记之前,您将看到数组$b每天包含type =>。

#6


1  

<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>

This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script

不应使用此代码。第一个CLI调用:/usr/bin/php phpscript。php将有一个argv值,即脚本的名称

array(2) {
   [0]=>
   string(13) "phpscript.php"
}

This one will always execute since will have 1 or 2 args passe

这个会一直执行,因为会有1或2个args

#7


1  

I strongly recommend the use of getopt.

我强烈建议使用getopt。

Documentation at http://php.net/manual/en/function.getopt.php

文档:http://php.net/manual/en/function.getopt.php

If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface

如果您想要为您的选项打印帮助,请查看https://github.com/c9s/GetOptionKit#通用-command-interface

#8


1  

To use $_GET so you dont need to support both if it could be used from command line and from web browser.

如果可以从命令行和web浏览器中使用$_GET,则不需要同时支持它。

if(isset($argv))
    foreach ($argv as $arg) {
        $e=explode("=",$arg);
        if(count($e)==2)
            $_GET[$e[0]]=$e[1];
        else    
            $_GET[$e[0]]=0;
    }

#9


-1  

if (isset($argv) && is_array($argv)) {
    $param = array();
    for ($x=1; $x<sizeof($argv);$x++) {
        $pattern = '#\/(.+)=(.+)#i';
        if (preg_match($pattern, $argv[$x])) {
            $key =  preg_replace($pattern, '$1', $argv[$x]); 
            $val =  preg_replace($pattern, '$2', $argv[$x]);
            $_REQUEST[$key] = $val;
            $$key = $val;
        }    
    }
}

I put parameters in $_REQUEST

我在$_REQUEST中输入参数

$_REQUEST[$key] = $val;

$ _REQUEST[$ key]= $ val;

and also usable directly

也可用直接

$$key=$val

关键= val美元美元

use this like that:

使用这个像这样:

myFile.php /key=val

myFile。php /关键=瓦尔

#1


97  

The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.

?type=daily参数(以$_GET数组结尾)仅对web访问页面有效。

You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).

您需要像调用myphp文件一样调用它。每日php并从$argv数组中检索该参数(它将是$argv[1],因为$argv[0]将是myfile.php)。

If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:

如果该页面也被用作网页,那么您可以考虑两个选项。或者使用shell脚本访问它,然后从cron调用wget:

#!/bin/sh
wget http://location.to/myfile.php?type=daily

Or check in the php file whether it's called from the commandline or not:

或者在php文件中检查它是否从命令行调用:

if (defined('STDIN')) {
  $type = $argv[1];
} else { 
  $type = $_GET['type'];
}

(Note: You'll probably need/want to check if $argv actually contains enough variables and such)

(注意:您可能需要/想要检查$argv是否包含足够的变量等)

#2


47  

Just pass it as normal parameters and access it in PHP using the $argv array.

只需将它作为普通参数传递,并使用$argv数组在PHP中访问它。

php myfile.php daily

and in myfile.php

而在myfile.php

$type = $argv[1];

#3


9  

These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:

这些行将转换CLI调用(如php myfile)的参数。php“type=daily&foo=bar”进入著名的$ _get数组:

if (!empty($argv[1])) {
  parse_str($argv[1], $_GET);
}

Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.

虽然覆盖全局$ _get数组是相当麻烦的,但是它可以快速地将所有脚本转换为接受CLI参数。

See http://php.net/manual/en/function.parse-str.php for details.

有关详细信息,请参阅http://php.net/manual/en/function.parse-str.php。

#4


6  

parameters send by index like other application

与其他应用程序一样,参数按索引发送

php myfile.php type=daily

and then you can gat them like this

然后你可以像这样做

<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
    echo $arg;
?>

#5


4  

Save this code in file myfile.php and run as php myfile.php type=daily

将此代码保存到文件myfile中。php和run作为php myfile。php类型=每天

<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg){
        if ($key > 0){
            list($x,$y) = explode('=', $arg);
            $b["$x"]    = $y;  
           }
       }
?>

If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

如果您添加var_dump($ b);在?>标记之前,您将看到数组$b每天包含type =>。

#6


1  

<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>

This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script

不应使用此代码。第一个CLI调用:/usr/bin/php phpscript。php将有一个argv值,即脚本的名称

array(2) {
   [0]=>
   string(13) "phpscript.php"
}

This one will always execute since will have 1 or 2 args passe

这个会一直执行,因为会有1或2个args

#7


1  

I strongly recommend the use of getopt.

我强烈建议使用getopt。

Documentation at http://php.net/manual/en/function.getopt.php

文档:http://php.net/manual/en/function.getopt.php

If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface

如果您想要为您的选项打印帮助,请查看https://github.com/c9s/GetOptionKit#通用-command-interface

#8


1  

To use $_GET so you dont need to support both if it could be used from command line and from web browser.

如果可以从命令行和web浏览器中使用$_GET,则不需要同时支持它。

if(isset($argv))
    foreach ($argv as $arg) {
        $e=explode("=",$arg);
        if(count($e)==2)
            $_GET[$e[0]]=$e[1];
        else    
            $_GET[$e[0]]=0;
    }

#9


-1  

if (isset($argv) && is_array($argv)) {
    $param = array();
    for ($x=1; $x<sizeof($argv);$x++) {
        $pattern = '#\/(.+)=(.+)#i';
        if (preg_match($pattern, $argv[$x])) {
            $key =  preg_replace($pattern, '$1', $argv[$x]); 
            $val =  preg_replace($pattern, '$2', $argv[$x]);
            $_REQUEST[$key] = $val;
            $$key = $val;
        }    
    }
}

I put parameters in $_REQUEST

我在$_REQUEST中输入参数

$_REQUEST[$key] = $val;

$ _REQUEST[$ key]= $ val;

and also usable directly

也可用直接

$$key=$val

关键= val美元美元

use this like that:

使用这个像这样:

myFile.php /key=val

myFile。php /关键=瓦尔