views:

13

answers:

0

Hi i am new to PHP .I am coded for Apple push Notification service on php. when i run the program it get error no 0.i am using SSH FTP server, but i connect to SSl FTP server. can any one help what i do for solving this error no 0.i used following code

<?php

 //include_once('jsonwrapper.php');
 //include_once('DBConnection.php');

 $msg = $_POST['msg'];
 //echo 'Message:'.$msg;
 //$apnsHost='gatway.sandbox.push.apple.com';
 //$apnsPort=2195; 
 $apnsCert='BAC_APNS.pem';
 $errno = null;
 $errstr = null;

 //echo'ssl://'.$apnsHost.':'.$apnsPort;
 // Create a stream to the server
 $streamContext = stream_context_create();
 stream_context_set_option($streamContext,'ssl','local_cert',$apnsCert);
 //echo'ssl://'.$apnsHost.':'.$apnsPort;

 $apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT, $streamContext);

 //echo $apns;
 if (!$apns) {
  echo "Error No: ".$err ."<br>"; 
  echo " Error:".$errstr."\n";
  return;
  }
 else {
  print "Connection OK\n";
  }
 //echo json_encode($msg);
 $payload['aps']= array('alert'=>$msg,'badge'=>1,'sound'=>'default');
 $payload= json_encode($payload);

 //Device token selection query
 $query = 'SELECT device_token FROM apns_Device_token';

 //Execute Selection query
 $result = mysql_query($query);
 $count = mysql_num_rows($result);
 echo 'Row Count:'.$count.'<br>';
 if($count!=0){
  while($deviceToken = mysql_fetch_array($result,MYSQL_ASSOC))
  { 
   $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken['device_token'])) . chr(0) . chr(strlen($payload)) . $payload;

   fwrite($apns, $apnsMessage);

   echo  '<br>'.array_shift(unpack("H*",$deviceToken['device_token'])).'<br>';
  }
  echo "<br><br>Apple push notification Server service message has sent to above Device Tokan's" ;
 }
 else {echo 'No Device Token Found';}

 socket_close($apns);
 fclose($apns);

 include('DB_close.php');
?>