title: PHP fails socket connection with Apple notification gateway
body: <p>Disclaimer: <strong>on SO I found many similar questions and some answers but none solved my issue</strong>.</p>
  
  <p>I have this easy PHP code:</p>
  
  <pre><code>&lt;?php
      $deviceToken = "myDeviceToken";
      $message = "Hello from server";
      $badge = 1;
      $sound = "default";
  
      // Construct the notification payload
      $body['aps'] = array(
          'alert' =&gt; $message
      );
  
      if ($badge) {
          $body['aps']['badge'] = $badge;
      }
  
      if ($sound) {
          $body['aps']['sound'] = $sound;
      }
  
      /* End of Configurable Items */
      $ctx = stream_context_create();
      stream_context_set_option($ctx, 'ssl', 'local_cert', 'my.pem');
  
      $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  
      if (!$fp) {
          print "Failed to connect $err $errstrn";
          return;
      } else {
          print "Connection OK\n";
      }
  
      $payload = json_encode($body);
      $msg = chr(0) . pack("n", 32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n", strlen($payload)) . $payload;
      print "sending message :" . $payload . "\n";
      fwrite($fp, $msg);
      fclose($fp);
  ?&gt;
  </code></pre>
  
  <p><strong>When called from browser I receive:</strong></p>
  
  <blockquote>
    <p>[13-Dec-2011 18:52:52] PHP Warning:  stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages:
    error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/srv/public_html/myTest.php on line 28</p>
    
    <p>[13-Dec-2011 18:52:52] PHP Warning:  stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/srv/public_html/myTest.php on line 28</p>
    
    <p>[13-Dec-2011 18:52:52] PHP Warning:  stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/srv/public_html/myTest.php on line 28</p>
  </blockquote>
  
  <p>Any idea of what's going on?</p>