php ios 群体推送
描述:用php 做服务器端,向IOS的APNS推送通知,附上代码
public static function apple_push($data,$deviceToken){<br /> $passphrase='123456';<br /> $ckfile="ck.pem";<br /> $ctx = stream_context_create();<br /> stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);<br /> stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);<br /> $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br /> //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br /><br /> if(!$fp){<br /> return false;<br /> }<br /> <br /> $payload=json_encode($data);<br /><br /> $pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;<br /> $result = fwrite($fp, $pushdata, strlen($pushdata));<br /> if(!$result){<br /> file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken."-".microtime().PHP_EOL,FILE_APPEND);<br /> }<br /> <br /> fclose($fp);<br />}
function iospush($tid,$subject,$pushinfo){<br /> $data['aps']=array(<br /> 'alert'=>$subject,<br /> 'sound'=>'default',<br /> 'type'=>'thread',<br /> 'tid'=>$tid,<br /> );<br /> $passphrase='123456';<br /> $ckfile="ck.pem";<br /> $ctx = stream_context_create();<br /> stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);<br /> stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);<br /> <br /> $payload=json_encode($data);<br /> foreach($pushinfo as $info){<br /> $deviceToken=$info['phoneToken'];<br /> $pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;<br /> $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br /> //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);//正式服务器<br /> if(!$fp){<br /> file_put_contents(dirname(__FILE__)."/pushlog.txt",$err.'-'.$errstr.'-60'.PHP_EOL,FILE_APPEND);<br /> continue;<br /> }<br /> $result = fwrite($fp, $pushdata, strlen($pushdata));<br /> if(!$result){<br /> file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken.PHP_EOL,FILE_APPEND);<br /> }<br /> fclose($fp);<br /> }<br />}