使用cronjob在php文件中运行功能 [英] Use cronjob to run function in php file

查看:98
本文介绍了使用cronjob在php文件中运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php文件(file1.php)中有一个功能。我正在尝试使用cronjob调用此函数。我的cron作业命令是:

I have a function in my php file (file1.php). I am trying to use cronjob to call this function. My cron job command is:

php /home/name/public_html/file1.php mail1

作为参考,该功能代码在下面,并且当直接从文件中调用时可以工作(即,该问题似乎与该功能无关)

For reference, the function code is below and works when called directly from the file (i.e., the issue does not appear to be with the function itself.

function mail1($runTotal, $accountArray){
    $to = $accountArray[0]['phoneNumber'] . '@vzwpix.com';
    $subject = '';
    $msg = "You have spent $" . $runTotal . " of $1,750 since the beginning of the month.;;
    $msg = wordwrap($msg);
    $headers = 'From: email@email.com';
    if (mail($to, $subject, $msg, $headers)) {
        echo $msg . " sent to " . $to;
    }
}

我用错误的方式调用函数吗?

Am I calling the function in the wrong way?

推荐答案

害怕说传递名称您希望调用PHP脚本的函数将不会在该脚本中自动运行该函数。 cript文件将需要逻辑来响应传递给它的任何命令行参数,以便正确执行。

Afraid to say that passing the name of the function you wish to call to the PHP script will not run the function within that script automatically. Your script file will need logic to respond to any command line arguments passed to it in order to execute correctly.

比方说 file1.php 其中定义了几个功能。只是一系列函数定义-函数外没有代码。如果您在命令行上运行该文件( $ php file1.php ),将发生的情况是PHP将加载该文件,解析您的函数,但是由于文件调用这些函数中的任何一个,脚本将到达末尾,执行将完成,并且实际上不会发生任何事情。

Let's say file1.php had several functions defined within it. Just a series of function definitions - no code outside of the functions. If you run that file on the command line ($ php file1.php), what will happen is PHP will load that file, parse your functions, but then since nothing in the file calls any of those functions, the script would reach the end, execution would complete, and nothing actually would have happened.

因此,要执行其中的一个函数,脚本将需要检查提供的任何命令行参数,然后做出适当的反应。

So in order to execute one of the functions, the script will need to inspect any command line arguments provided and then react appropriately.

在PHP中, $ argv 变量是一个数组,其中包含先运行的文件的名称,然后依次包含所有命令行参数。除非用引号引起来或适当地转义,否则空格将用作命令行参数的分隔符。如果命令是通过命令行运行的,则此变量在根作用域*中可用。

In PHP, the $argv variable is an array that contains first the name of the file that was run, and then any command line arguments in order. Unless enclosed in quotes or escaped properly, white space serves as the separators for command line arguments. If the command was run through the command line, this variable is available to you in the root scope*.

以下是PHP对 $ argv 变量:


包含从命令行运行时传递给脚本的所有参数的数组。

Contains an array of all the arguments passed to the script when running from the command line.

注意:第一个参数$ argv [0]始终是用于运行脚本的名称。

Note: The first argument $argv[0] is always the name that was used to run the script.

如果运行 php file1.php发送邮件--dry-run ,则 $ argv 变量将是(在JSON中显示): [ file1.php,发送,邮件,-试运行]

If you run php file1.php send mail --dry-run, then the $argv variable would be (shown here in JSON): ["file1.php", "send", "mail", "--dry-run"].

您可以执行此操作,将其放置在 file1.php 文件代码的末尾,该文件代码将查看该 $ argv 变量并采取适当的措施。在您的问题概述的特定情况下,请尝试在文件末尾添加以下内容:

What you can do with this is place at the end of your file1.php file code that looks at that $argv variable and takes an appropriate action. In the specific case outlined in your question, try adding something like this to the end of your file:

//If we have a command line argument
if (!empty($argv[1])) {
    switch ($argv[1]) {
        case "mail1":
            mail1(); //Call our mail() function
            echo "\n\nCalled function mail1()\n\n"; //Print what we did to the console
            break;
    }
}

现在,当您开心 php file1.php mail1 (这最终是您的CRON工作正在做的事情),第一个参数( $ argv [1] )是 mail1,因此<执行switch语句中的code> mail1 案例,该案例调用您的 mail1 函数并将有用的注释写入控制台,以便您知道

Now when you fun php file1.php mail1, which is ultimately what your CRON job is doing, that first argument ($argv[1]) is "mail1", so the mail1 case in the switch statement is executed, which calls your mail1 function and writes to the console a helpful note so you know it did what you asked.

有用的链接:

  • http://php.net/manual/en/reserved.variables.argv.php
  • http://php.net/manual/en/features.commandline.php

希望这会有所帮助!

*从根本上讲,我的意思是它不是全局的,因此除非将其传递给函数或类,否则您不能在函数或类中访问它,但在它们外部可以访问它。在上面的代码示例中,由于该代码位于函数或类的外部,因此它可以访问 $ argv

* By root scope I mean that it is not global, so you can't access it inside a function or a class unless you pass it along to them, but outside of them you have access to it. In my code example above, since that code is outside of a function or a class, it can access $argv.

这篇关于使用cronjob在php文件中运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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