将变量传递给从命令行运行的php脚本 [英] Pass variable to php script running from command line

查看:123
本文介绍了将变量传递给从命令行运行的php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php文件,需要从命令行运行(通过crontab)。但我需要传递'type = daily'到文件,但我不知道how.i尝试

  php myfile。 php?type = daily 

但此错误是结果:


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


$ <$ p $ <$>


解决方案

$ _ GET 数组中)仅对Web访问的页有效。



您需要像 php myfile.php daily 一样调用它,并从<$ c $ argv [0] <$ cv> $ argv code>将 myfile.php )。



如果网页也用作网页,有两个选项,你可以考虑。使用shell脚本和wget访问它,并从cron调用它:

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

或检入PHP档案是否从命令行调用:

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

(注意:您可能需要/想检查 $ argv 实际上包含足够的变量,如


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

php myfile.php?type=daily

but this error was result:

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

what can i do?

解决方案

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

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).

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:

#!/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:

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)

这篇关于将变量传递给从命令行运行的php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆