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.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题