2

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 &");

function(sendNotificaiton())ここでfile(Notification.php)、引数を呼び出してパラメーターとして渡すにはどうすればよいでしょうか: $token と $count ?

exec/shell_exec/passthru commandsそれが私を助けることができることがわかりました。しかし、この場合、これら 3 つのコマンドのうち、どのコマンドを使用したのでしょうか?

これについて私を案内してください。どんな助けでも大歓迎です。

4

5 に答える 5

6

もう 1 つ 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 トークン数 >> /path/to/log_file.log 2>&1 &");

于 2013-09-04T11:40:57.993 に答える
2

コマンドライン実行を呼び出すためだけに別のスクリプトを用意したくありません。

testNotification.php

<?php
include_once 'Notification.php';
use Bubba\Util\Notification;

$Notification = new Notification();
$Notification->sendNotification('some1token', 33);

これは、Notification クラス ファイルが同じディレクトリにあることを前提としていますが、Web アクセスできないライブラリ ディレクトリにあることが予想されます。

通知.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 関数を呼び出します。通知機能は、それが CLI 呼び出しではないことを認識するため、exec 呼び出しを行い、すぐに戻ります。exec 呼び出しは Notifcation.php ファイルをロードし、それが CLI から実行されていることを認識するため、それ自体をインスタンス化し、適切なコマンド ライン オプションを取得し、通知を送信して終了します。

適切な通知メッセージと同じディレクトリに新しい notification.log があることを確認することで、これを確認できます。

于 2013-09-10T16:30:22.247 に答える
1

Linux Webサーバーを使用したCPanelでは、以下のコードを使用しました

A)

/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 が機能します。両方を試すことができます。お役に立てば幸いです。

于 2013-09-09T13:06:19.100 に答える