2

我想在某些情况下在后台运行以下代码:

档案一:

// ------------------- Some Code Before Following -----------------------

// send notification to given device token
$notification = new Notification();                    
$notification->sendNotification($token,$count);

// ------------------- Some Code After Above ------------------------

这将调用以下类:

// Here I am sending notification to given device token (APNS Push Notification) 
<?php

class Notification
{
    public function sendNotification($token,$count)
    {
        if(strlen($token)%16 == 0)
        {
            // set Device token
            $deviceToken = $token;
            $passphrase = 'xxxxx';
            $badge = $count;

            // Displays alert message here:
            $message = 'Hello! There.';

            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

            // Open a connection to the APNS server
            $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
            if (!$fp)
                exit("Failed to connect: $err $errstr" . PHP_EOL);

            echo("Connected to APNS.");

            // Create the payload body
            $body['aps'] = array(
                                 'alert' => $message,
                                 'badge' => $badge,
                                 'sound' => 'default'
                                 );

            // Encode the payload as JSON
            $payload = json_encode($body);

            // Build the binary notification
            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

            // Send it to the server
            $result = fwrite($fp, $msg, strlen($msg));

            if (!$result)
                echo("\n\nMessage not delivered.\n\n");
            else
                echo("\n\nMessage successfully delivered.\n\n");

            // Close the connection to the server
            fclose($fp);
        }
    }
}


?>

Execution这门课什么时候到,它completedit will go back to file Acontinue execution after sending notification.

但在这里它会take time to send notification (around 3-5-7 seconds)和那个时候user has to wait which is not a good idea

所以我想要做的是send notification in Background process instead of main thread。所以用户不必等待不必要的。

Please note that I don't want to use Cron.

我发现这exec/shell_exec/passthru command可以帮助我。但是out of these three commands在这种情况下我使用了哪个命令?

请指导我。任何帮助将不胜感激。

4

3 回答 3

5
passthru("/usr/bin/php /path/to/yourFile.php >> /path/to/log_file.log 2>&1 &");

这里有几件事很重要。

首先:输入 php 二进制文件的完整路径,因为此命令将在 apache 用户下运行,并且您可能不会在该用户中设置 php 之类的命令别名。

第二:注意命令字符串末尾的两件事:the2>&1&. 用于将2>&1错误重定向到标准 IO。最重要的是&命令字符串的末尾,它告诉终端不要等待响应。

于 2013-09-04T07:49:48.507 回答
0

这里有几个选项:

  • 您可以使用 pcntl_fork ( http://php.net/manual/en/function.pcntl-fork.php )分叉一个单独的 PHP 进程
  • 您可以使用 cronjob 来检查您不想检查的未发送修改(您没有提到 WHY,这就是我提到它的原因)
  • 您可以为此使用作业队列系统。迄今为止最好的解决方案,但需要一些时间才能开始。Gearman 是我有过的经验,它就像一个魅力。
于 2013-09-04T07:37:51.227 回答
0

按照此处的说明在后台运行 PHP 脚本:

php执行一个后台进程

它需要使用 shell_exec 从文件 A 调用脚本,因此您必须为要调用的代码创建一个新文件。它还假设您使用的是 linux

于 2013-09-04T07:40:28.200 回答