问题
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:
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:
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.
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.
来源:https://stackoverflow.com/questions/19183760/500-error-when-sending-a-lot-of-push-notifications-php