dongman5539 2013-10-04 14:27
浏览 32
已采纳

500发送大量推送通知时出错,PHP?

I'm using the following code to cycle through a database containing the device ID's that my ios app collects. At around 300 or so notifications my server freezes in a 500 error. Any ideas how I can do this more efficiently? I am on a shared server; would my only option be to set this up on a dedicated machine?

php.ini has the following set -

set_time_limit = 999999999
max_execution_time = 999999999
memory_limit = 512M

Here's the code -

if(isset($_POST['pushNotify'])){ /// PROCESS PUSH NOTIFICATION TO LAST 120 DAYS OF ACTIVE USERS
$message = stripslashes(strip_tags(trim($_POST['message'])));

mysql_connect("URL", "USER", "PASS") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());
$cutoff = time() - (60*60*24*120);
$result = mysql_query("SELECT * FROM devices WHERE timestamp > '$cutoff' ORDER BY timestamp DESC");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../certs/distck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

while($row = mysql_fetch_array($result))
  {
  if(strlen(trim($row['deviceid']))>25){
    $devid = str_replace("-", "", trim($row['deviceid']));
    $payload = '{
                    "aps" : 

                        { "alert" : "'.$message.'",
                          "badge" : 0,
                          "sound" : "bingbong.aiff"
                        } 
                }';


    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $devid)) . pack      ("n",strlen($payload)) . $payload;
    if(!@fwrite($fp, $msg)){
        @fclose($fp);
        $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        @fwrite($fp, $msg);
        }
    }
    @fclose($fp);

} /// END PUSH NOTIFY
  • 写回答

1条回答 默认 最新

  • douwei2966 2013-11-06 08:12
    关注

    I would advise you remove the @ notation. !@fwrite should be avoided. fwrite returns FALSE if it fails or if the connection is severed which is exactly what you need to know. Reminder: Apple breaks the connection as soon as any error comes up.

    Issues:

    1. If an fwrite fails, you have the right idea: close socket, re-open, try again; except that Apple can close the connection if you send an invalid token (which is often the case for many reasons). Hence, you should not retry the same token but move on instead to the next one.

    2. If fwrite fails a second time (in your retry condition), you do not catch the error and at that point you have no sockets open which might explain your 500 error. Consider re-writing the logic around that to retry N times before moving on.

    If you use the binary interface (which is what Apple recommends now), you can actually get exact error notices from Apple. You can read more about that here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致