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