2012-04-02 60 views
1

我正在构建一个iPhone推送服务器,并试图推动工作。我有一个message.php文件,它将新消息放入数据库中,然后将消息添加到数据库中的push_queue表中。调用另一个php文件

要发送推送,我手动必须去浏览器并调用推送文件(../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);   
    } 
} 
+0

您的推送如何工作?它是彗星还是你的Iphone应用程序每隔x秒钟调用一次服务器来查询新消息? – 2012-04-02 13:11:30

+0

感谢您的回复..该应用程序每隔x秒钟就会调用一次服务器来查询。 – BlackMouse 2012-04-02 13:13:27

+1

好的,那么push.php是什么?一些代码示例会更好。 – 2012-04-02 13:14:32

回答

0

创建,做一个函数或类的一切,你push.php做,并呼吁它收到新邮件时或当iPhone应用程序查询新消息时。在这种情况下,你不需要在message.php中调用其他PHP。

这是MVC的概念,即将您的业务逻辑与控制器分开。在这种情况下推送是一个业务逻辑和message.php和push.php是你的控制器。