title: Where to correctly place and name .pem file for apple notification in php
body: <p>My cron files are under cron folder which is situated at the root directory. But it is always showing an error as - </p>
  
  <pre><code>Warning: stream_socket_client(): SSL: crypto enabling timeout in /home/myname/beta.myhost.com/cron/dailycron.php on line 43
  
  Warning: stream_socket_client(): Failed to enable crypto in /home/myname/beta.myhost.com/cron/dailycron.php on line 43
  
  Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/myname/beta.myhost.com/cron/dailycron.php on line 43
  </code></pre>
  
  <p>Line <code>43</code> is around -</p>
  
  <pre><code>$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
  </code></pre>
  
  <p>My code is as follows -</p>
  
  <pre><code>&lt;?php
  
  //############################# Database connection ################################/
  
  //######################### Daily notification code ############################################/
  
  $result = mysql_query("SELECT DISTINCT EntryId, twittes, groupname, created_at FROM users_twitte NATURAL JOIN (SELECT EntryId, MAX( created_at ) created_at FROM users_twitte GROUP BY EntryId) t");
  while( $row = mysql_fetch_array($result) ) {
  
    // Get user array from EntryId
    $rowdata = $row['EntryId'];
  
    $result2 = mysql_query("SELECT * FROM `users_subscription` WHERE EntryId = $rowdata");
  
    while( $row2 = mysql_fetch_assoc($result2) ) {
      $group_name = $row['groupname'];
      $grp_usr_id = $row2['uid'];
      send_notification($group_name, $grp_usr_id);
    }
  }
  
  // Send Notification
  function send_notification($groupname, $uid) {
  
      // GET device token from uid
      //$device_token = get_device_token($uid);   
  
      $payload = array();
      $payload['aps'] = array('alert' =&gt; "THERE IS AN MESSAGE FROM $groupname", 'badge' =&gt; 0, 'sound' =&gt; 'default');
      $payload = json_encode($payload);
  
      $apns_url = NULL;
      $apns_cert = NULL;
      $apns_port = 2195;
  
      $apns_url = 'gateway.sandbox.push.apple.com';
      $apns_cert = '/home/myname/beta.myhost.com/cron/ck.pem';
  
      $stream_context = stream_context_create();
      stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
      stream_context_set_option($stream_context, 'ssl', 'passphrase', "12345");
  
      $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
  
      $get_token_info = get_device_token_and_type($uid);
      if( 1 === $get_token_info['status'] ) {
          if(0 === $get_token_info['device_type']) {
              $device_token = $get_token_info['$device_token'];   
              $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
          }
          fwrite($apns, $apns_message);
          @socket_close($apns);
          @fclose($apns);
      }
  }
  
  // GET device token for uid
  function get_device_token_and_type($uid) {
      $dev_tok_type_arr = array();
      // GET EMAIL id from uid
      $result = mysql_query("SELECT LoginData.device_token, LoginData.device_type FROM LoginData INNER JOIN users ON LoginData.userid = users.email WHERE users.uid =$uid");
      if( mysql_num_rows($result) &gt; 0 ) {
          while( $rows = mysql_fetch_assoc($result) ) {
              $dev_tok_type_arr = array('device_token'=&gt; $rows['device_token'], 'device_type'=&gt; $rows['device_type'], 'status'=&gt; 1);
          }
      }
      else {
          $dev_tok_type_arr = array('status'=&gt;0, 'message'=&gt; 'No device token exists for the userid');
      }
      return $dev_tok_type_arr;
  }
  
  ?&gt;
  </code></pre>
  
  <p>My .pem file is in same cron folder and I mentioned absolute path at - <code>$apns_cert</code> . Let me know what I am doing wrong, I checked passphrase which is correct.</p>