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

Pass a variable to a PHP script running from the command line(将变量传递给从命令行运行的 PHP 脚本)
本文介绍了将变量传递给从命令行运行的 PHP 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

php myfile.php?type=daily

但是返回了这个错误:

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

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

我能做什么?

推荐答案

?type=daily 参数(以 $_GET 数组结尾)仅对网页访问页面.

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

您需要像 php myfile.php daily 一样调用它,并从 $argv 数组(这将是 $argv[1],因为 $argv[0] 将是 myfile.php).

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

如果页面也用作网页,您可以考虑两种选择.使用 shell 脚本和 Wget 访问它,然后从 cron:

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

或者检查PHP文件是否从命令行调用:

Or check in the PHP file whether it's called from the command line or not:

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

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

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)