500 发送大量推送通知时出错,PHP? [英] 500 Error when sending a lot of push notifications, PHP?

查看:27
本文介绍了500 发送大量推送通知时出错,PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码循环访问包含我的 ios 应用程序收集的设备 ID 的数据库.在大约 300 个左右的通知中,我的服务器在 500 错误中冻结.任何想法如何更有效地做到这一点?我在共享服务器上;我唯一的选择是在专用机器上进行设置吗?

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 有以下设置 -

php.ini has the following set -

set_time_limit = 999999999
max_execution_time = 999999999
memory_limit = 512M

这是代码-

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

推荐答案

我建议您删除 @ 符号.!@fwrite 应该避免.fwrite 返回 FALSE 如果失败或者连接被切断,这正是你需要知道.提醒:Apple 会在出现任何错误时立即断开连接.

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.

问题:

  1. 如果 fwrite 失败,你有正确的想法:关闭套接字,重新打开,再试一次;除了 Apple 可以关闭连接,如果您发送无效的令牌(出于多种原因,经常出现这种情况).因此,你不应该重试相同的令牌,而是继续进行下一个.

  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.

如果 fwrite 第二次失败(在您的重试条件下),您不会捕获错误,并且此时您没有打开的套接字,这可以解释您的 500 错误.考虑重新编写逻辑,在继续之前重试 N 次.

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.

如果您使用二进制接口(这是 Apple 现在推荐的),您实际上可以从 Apple 获得准确的错误通知.您可以在此处阅读更多相关信息.

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.

这篇关于500 发送大量推送通知时出错,PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆