1

我有一个用 PHP 编写的 Web 服务,一个 iPhone 应用程序连接到该服务。当应用程序调用该服务时,一系列通知消息会发送到 Apple 的 APNs 服务器,以便它可以向应用程序的其他用户发送推送通知。

在某些情况下,此过程可能会很耗时,并且我的应用程序必须等待很长时间才能获得响应。响应完全独立于发送到 APNs 服务器的通知消息的结果。

因此,我希望 Web 服务将响应发送回应用程序,无论是否已向 APNs 发送消息。

我当前的代码:

<?
    <...>
    echo $chatID; // This has to be printed immediately
    include('apns.php'); // Just invoke this, dont wait for response! 
?>

我该怎么做呢?

4

2 回答 2

3

您应该关闭连接,然后像这样继续处理:

set_time_limit(0); 
ignore_user_abort(true);  

//start the output buffer
ob_start();

//process what needs to be returned to browser
echo $chatID;

//tell the browser not to expect any more content and close the connection
header("Content-Length: " . ob_get_length());
header("Connection: close");
ob_end_flush(); 
ob_flush(); 
flush(); 
//continue processing
于 2012-07-26T11:11:25.647 回答
2

打电话flush();;这将确保之前打印的任何数据都立即发送给客户端。如果您使用输出缓冲,您需要先调用ob_flush();

于 2012-07-26T11:04:52.987 回答