簡體   English   中英

調用另一個php文件

[英]Call another php file

我正在構建iPhone推送服務器,並嘗試使推送工作。 我有一個message.php文件,它將新消息放入數據庫中,然后將消息添加到數據庫中的push_queue表中。

要發送推送,我必須手動進入瀏覽器並調用推送文件(../push/push.php),以發送推送。

有什么方法可以自動從message.php文件中調用push.php文件嗎?

我嘗試了require_oneincludeexecfile_get_contents沒有任何運氣。

如果我使用它會起作用:

header('Location: ../push/push.php');

但是,push.php文件需要花費幾秒鍾的時間來執行和完成,因此用戶嘗試發送消息時會有延遲。

我想我可以使用cron作業來調用push.php文件,但我寧願不這樣做。

這是push.php中的核心功能(基於http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2 ):

function start()
{
    //writeToLog('Connecting to ' . $this->server);

    if (!$this->connectToAPNS())
        exit;


    while (true)
    {
        // Do at most 20 messages at a time. Note: we send each message in
        // a separate packet to APNS. It would be more efficient if we 
        // combined several messages into one packet, but this script isn't
        // smart enough to do that. ;-)

        $stmt = $this->pdo->prepare('SELECT * FROM push_queue WHERE time_sent IS NULL LIMIT 20');
        $stmt->execute();
        $messages = $stmt->fetchAll(PDO::FETCH_OBJ);

        $deletedIds = array();

        foreach ($messages as $message)
        {
            if ($this->sendNotification($message->message_id, $message->device_token, $message->payload))
            {
                //$stmt = $this->pdo->prepare('UPDATE push_queue SET time_sent = NOW() WHERE message_id = ?');
                //$stmt->execute(array($message->message_id));

                 $deletedIds[] = $message->message_id;

                //$stmt = $this->pdo->prepare('DELETE FROM push_queue WHERE message_id = ?');
                //$stmt->execute(array($message->message_id));

            }
            else  // failed to deliver
            {
                $this->reconnectToAPNS();
            }
        }

        //Delete the chunk of messages.
        $this->pdo->query('DELETE FROM push_queue WHERE message_id IN ('.implode(',', $deletedIds).')');

        unset($messages);           
    }
}

創建一個函數或類來完成push.php的所有工作,並在收到新消息或iPhone應用程序查詢新消息時調用它。 在這種情況下,您無需在message.php中調用其他PHP。

這是MVC的概念,即將業務邏輯與控制器分離。 在這種情況下,推送是一種業務邏輯,而message.php和push.php是您的控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM