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

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

问题描述

我用下面的code到循环包含设备ID的,我的iOS应用程序收集的数据库。在大约300左右通知我的服务器冻结在一个500错误。任何想法如何,我可以更有效地做到这一点?我是一个共享的服务器上;将我唯一的选择是设置此专用计算机上?

php.ini中有以下组 -

 参数或者set_time_limit = 999999999
的max_execution_time = 999999999
memory_limit的= 512M

这里的code -

 如果(使用isset($ _ POST ['pushNotify'])){///过程推送通知将持续活跃用户数120天
$消息=的stripslashes(用strip_tags(修剪($ _ POST ['消息'])));mysql_connect(URL,USER,PASS)或死亡(mysql_error());
mysql_select_db(数据库)或死亡(mysql_error());
$截止时间=() - (60 * 60 * 24 * 120);
$结果= mysql_query(SELECT * FROM WHERE设备时间戳>'$截止'ORDER BY的时间戳降序);
$ CTX = stream_context_create();
stream_context_set_option($ CTXSSL,的local_cert',​​'../certs/distck.pem');
stream_context_set_option($ CTX,'SSL','密​​码','密码');
$计划生育=在stream_socket_client('SSL://gateway.push.apple.com:2195',$犯错,$ errstr,60,STREAM_CLIENT_CONNECT,$ CTX);而($行= mysql_fetch_array($结果))
  {
  如果(strlen的(修剪($行['DEVICEID']))> 25){
    $ DEVID = str_replace函数( - ,,修剪($行['DEVICEID']));
    $负载='{
                    APS:                        {提醒:'$消息。'
                          徽章:0,
                          声音:bingbong.aiff
                        }
                };
    $味精= CHR(0)。包(N,32)。包(H *',str_replace函数('','',$ DEVID))。包(N的strlen($有效载荷))。 $有效载荷;
    如果(!@的fwrite($计划生育,$味精)){
        @fclose($ FP);
        $计划生育=在stream_socket_client('SSL://gateway.push.apple.com:2195',$犯错,$ errstr,60,STREAM_CLIENT_CONNECT,$ CTX);
        @fwrite($ FP,$味精);
        }
    }
    @fclose($ FP);} /// END PUSH NOTIFY


解决方案

我建议你删除@符号。 !@ FWRITE 应尽量避免。 FWRITE 如果失败或者如果连接将被切断,这正是返回FALSE你需要知道。提醒:苹果马上断开连接任何错误出现

问题:


  1. 如果一个FWRITE失败,你有正确的观念:关闭插座,重新打开,
    再试一次;除了苹果可以关闭连接,如果你发送
    无效的令牌(通常是有很多原因的情况下)。因此,你
    不应该重试同样的道理,而是移动到下一个。


  2. 如果fwrite的第二次失败(在重试条件),你不捕获错误,并在这一点上你没有插座开这或许可以解释你的500错误。考虑重新写周围的逻辑在继续之前重试了N次。


如果您使用二进制接口(这是苹果公司现在建议),其实你可以得到来自苹果真正的错误通知。你可以阅读更多关于<一个href=\"https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html\"相对=nofollow>这里。

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

解决方案

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.

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

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