通过PHP推送到多个iOS设备

To say the good thing first - my code is working. But unfortunately, it is extremly slow with many devices to push to. As the whole pushing process failed when one device failed with my code before (the connection was made outside the loop), I took the connection inside my loop:

foreach ($deviceTokens as $token) {
  $fp = stream_socket_client($this->data['config']['push']['apnsAddress'], $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
  $msg = chr(0).pack('n', 32).pack('H*', $token).pack('n', strlen($payload)).$payload;
  $result = fwrite($fp, $msg);
  if (!$result) {
    $this->pushLog('devicetoken '.$token.' failed');
    $errors++;
  }
  fclose($fp);
}

Isn't there any other way to push to multiple devices than making a new connection for every single person that has an app installed? The runtime for this script is just way to long at the moment.

First things first, may I know how many devices are you trying to push in the loop? The code you have should work fine for reasonable amounts of tokens. How fast is your server-to-apple connection? An alternative would be to use a third-party push API. Normally, they offer extended parameters and options, which surely include push to multiple devices with a single call. Cheers.