title: PHP Sending Push Notifications Failed to connect: 111 Connection refused
body: <p>I am trying to setup push notifications, I got my certificates from apple developer, unabled pushed notifications on my ios app and I have a PHP script that is suppose to send a push notification. My hosting provider whitelisted the port 2195 outbound for my account. But push notifications are still not working, I get this error</p>
  
  <blockquote>
    <p>Failed to connect: 111 Connection refused</p>
  </blockquote>
  
  <p>I am using Development Certificates and they are enabled in my apple developer account.</p>
  
  <pre><code>&lt;?php 
  
  class PushNotifications {
  
      private static $passphrase = 'PASSPHRASE';
  
      public function iOS($data, $devicetoken) {
  
          $deviceToken = $devicetoken;
  
          $ctx = stream_context_create();
  
          stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
          stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
  
          $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);
          }
  
          $body['aps'] = array('alert' =&gt; array('title' =&gt; $data['mtitle'], 'body' =&gt; $data['mdesc'],),'sound' =&gt; 'default');
  
          $payload = json_encode($body);
  
          $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  
          $result = fwrite($fp, $msg, strlen($msg));
  
          fclose($fp);
  
          if (!$result)
              return 'Message not delivered' . PHP_EOL;
          else
              return 'Message successfully delivered' . PHP_EOL;
  
      }
  
      private function useCurl(&amp;$model, $url, $headers, $fields = null) {
  
          $ch = curl_init();
          if ($url) {
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, true);
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
              if ($fields) {
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
              }
              $result = curl_exec($ch);
              if ($result === FALSE) {
                  die('Curl failed: ' . curl_error($ch));
              }
  
              curl_close($ch);
  
              return $result;
          }
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Can anyone help?</p>