如何从当前文件中使用exec命令调用另一个文件的函数


How to call a function of another file by using exec command from current file

我有一个文件,Notification.php,这是一个类。它的结构是这样的:

Class Notificaiton
{
    public function sendNotification($token,$count)
    {
       // Here is the code for sending push notification(APNS)
    }      
}

通常,如果我想从另一个PHP文件中调用这个函数,那么我必须创建一个类的对象,然后调用它的方法,如下所示:

$notification = new Notification();   
$notification->sendNotification($token,$value);

但是我想做的是在后台进程中调用这个。所以我使用exec()命令如下:

exec("/usr/bin/php /path/to/Notification.php >> /path/to/log_file.log 2>&1 &");

我想知道如何调用file(Notification.php)function(sendNotificaiton())并将参数作为参数传递:$token和$count ?

我发现exec/shell_exec/passthru commands可以帮助我。但是在这种情况下,我使用了这三个命令中的哪一个呢?

请指点我一下。

最好再创建一个php文件并在其中调用该方法。像

notificationCall.php:

<?php
include 'notification.php';
$token = $argv[1];
$count = $arg2[2];
$notification = new Notification();   
$notification->sendNotification($token,$count);
?>

exec("/usr/bin/php/path/to/notificationCall.php token count>>/path/to/log_file.log 2>&1 &");

为Notification类添加构造方法,如下所示:

function __construct() {
    if(isset($_GET['action']) && $_GET['action']=='sendnotification') {
        $this->sendNotification();
    }
}

然后像这样运行exec命令:

exec("/usr/bin/php /path/to/Notification.php?action=sendnotification >>
/path/to/log_file.log 2>&1");

$token和$count也可以作为exec命令中的GET参数提供。

我不希望有一个单独的脚本仅仅调用命令行执行。

testNotification.php

<?php
include_once 'Notification.php';
use Bubba'Util'Notification;
$Notification = new Notification();
$Notification->sendNotification('some1token', 33);

这里假设我们的Notification类文件在同一个目录中,尽管它应该在一个不能通过web访问的库目录中。

Notification.php

<?php
namespace Bubba'Util;
if (Notification::isCommandLineInterface()){
    $shortopts  = "";
    $shortopts .= "t:";  // The Token, Required value
    $shortopts .= "c:";  // The Count, Required value
    $options = getopt($shortopts);
    $Notification = new Notification();
    $Notification->sendNotification($options['t'], $options['c']);
    exit;
}
class Notification {
    protected $_token;
    protected $_count;
    public function __construct() {
    }
    public function sendNotification($token = NULL, $count = NULL){
        $this->setCount($count);
        $this->setToken($token);
        // If we are running from the command line
        // then we just want to send the notification
        if ($this->isCommandLineInterface()){
            print "I am notifying you with [{$this->_token}] that the count is [{$this->_count}]'n";
        }else{
            $cmd = '/usr/bin/php ' . __FILE__ . " -t='{$this->_token}' -c={$this->_count} >> notification.log 2>&1 &";
            exec($cmd );
        }
    }
    /**
     * Do some appropo validation, you don't want stuff injected
     * @param string $token
     */
    protected function validateToken($token){
        if (empty($token) || !is_string($token)){
            $this->_token = NULL;
        }
    }
    /**
     * Do some appropo validation, you don't want stuff injected
     * @param string $token
     */
    protected function validateCount($count){
        if (empty($count) || !is_numeric($count)){
            $this->_count = 0;
        }
    }

    /**
     * Determine if this is running on the command line or not.
     * @return boolean
     */
    public static function isCommandLineInterface(){
        return (php_sapi_name() === 'cli');
    }
    /**
     * @return the $_token
     */
    public function getToken() {
        return $this->_token;
    }
    /**
     * @return the $_count
     */
    public function getCount() {
        return $this->_count;
    }
    /**
     * @param NULL $_token
     */
    public function setToken($_token) {
        $this->validateToken($_token);
        $this->_token = $_token;
    }
    /**
     * @param number $_count
     */
    public function setCount($_count) {
        $this->validateCount($_count);
        $this->_count = $_count;
    }
}

在本例中,您只需浏览http://your.localhost.net/testNotification.php, testNotification将实例化Notification对象,并调用notify函数。notify函数将意识到它不是CLI调用,因此它将调用exec并立即返回。exec调用将加载notification .php文件,并意识到它是从CLI运行的,因此它将实例化自身,获取适当的命令行选项,发送通知,然后退出。

您可以通过在具有适当通知消息的同一目录中注意到一个新的notification.log来验证。

你的例子很简单,也许这是用火箭筒杀死一只苍蝇,但如何像http://www.gearman.org/或http://www.rabbitmq.com/。

在CPanel与linux web服务器我使用下面的代码

)

/usr/bin/php -q /home/username/public_html/path/to/Notification.php

和B)

 lynx --dump http://yourlink.com/path/to/Notification.php > /path/to/log_file.log

对我来说,在一些服务器工作的方法A和在一些服务器工作的方法b。你可以尝试两种。