title: Send iOS push notification from PHP
body: <p>I'm working on an iOS app that use iOS Push Notifications. I want to send the notification from a php script on my server. The code in my app works really good for registering for remote notification and receive them. I use this php script to send notification and it works well too:</p>
  
  <pre><code>&lt;?php
  
  // My device token here (without spaces):
  $deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  
  // My private key's passphrase here:
  $passphrase = 'myPrivateKey';
  
  // My alert message here:
  $message = 'New Push Notification!';
  
  //badge
  $badge = 1;
  
  $ctx = stream_context_create();
  stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  
  // Open a connection to the APNS server
  $fp = stream_socket_client(
      'ssl://gateway.sandbox.push.apple.com:2195', $err,
      $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  
  if (!$fp)
  exit("Failed to connect: $err $errstr" . PHP_EOL);
  
  echo 'Connected to APNS' . PHP_EOL;
  
  // Create the payload body
  $body['aps'] = array(
      'alert' =&gt; $message,
      'badge' =&gt; $badge,
      'sound' =&gt; 'newMessage.wav'
  );
  
  // Encode the payload as JSON
  $payload = json_encode($body);
  
  // Build the binary notification
  $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  
  // Send it to the server
  $result = fwrite($fp, $msg, strlen($msg));
  
  if (!$result)
      echo 'Error, notification not sent' . PHP_EOL;
  else
      echo 'notification sent!' . PHP_EOL;
  
  // Close the connection to the server
  fclose($fp);
  </code></pre>
  
  <p>My problem is that if i run this script by terminal on my imac, all works good and notifications are sent, but if i upload it on my server it doesn't work. My certificate is valid and it's in the same folder of the script. When i try to run the script on the server, i add this line to include the certificate file and use the variable $cerificate:</p>
  
  <pre><code>$certificate = file_get_contents('ck.pem');
  </code></pre>
  
  <p>This is the error of the php server side:</p>
  
  <pre><code>Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in script.php on line 29
  Failed to connect: 110 Connection timed out
  </code></pre>
  
  <p>Why this happens? Do i have to add something on my server to enable pans connection?</p>
  
  <p>-----UPDATE QUESTION-----</p>
  
  <p>My first problem was that i didn't have the privileges to send connection on port 2195 and receive response on port 2196. Now, my server admin enabled these connection and i have different errors:</p>
  
  <pre><code>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 script.php on line 28
  
  Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in script.php on line 28
  
  Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in script.php on line 28
  </code></pre>
  
  <p>Failed to connect: 0</p>