Rumah >pembangunan bahagian belakang >tutorial php >php ios 群体推送
描述:用php 做服务器端,向IOS的APNS推送通知,附上代码
public static function apple_push($data,$deviceToken){ $passphrase='123456'; $ckfile="ck.pem"; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if(!$fp){ return false; } $payload=json_encode($data); $pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $pushdata, strlen($pushdata)); if(!$result){ file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken."-".microtime().PHP_EOL,FILE_APPEND); } fclose($fp);}
function iospush($tid,$subject,$pushinfo){ $data['aps']=array( 'alert'=>$subject, 'sound'=>'default', 'type'=>'thread', 'tid'=>$tid, ); $passphrase='123456'; $ckfile="ck.pem"; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $payload=json_encode($data); foreach($pushinfo as $info){ $deviceToken=$info['phoneToken']; $pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);//正式服务器 if(!$fp){ file_put_contents(dirname(__FILE__)."/pushlog.txt",$err.'-'.$errstr.'-60'.PHP_EOL,FILE_APPEND); continue; } $result = fwrite($fp, $pushdata, strlen($pushdata)); if(!$result){ file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken.PHP_EOL,FILE_APPEND); } fclose($fp); }}
这种东西肯定需要用异步去实现
试试xmpphp,使用聊天服务器来实现,可能更靠谱一些。
是异步的,但是每次打开,关闭,太浪费时间,不能在半小时内推送完。
用xmpphp倒是没考虑过,既然是异步推送,不用持久连接。
现在我的解决办法是:用pcntl,开十个进程执行
要快就只能?多?程了。
感谢各位的回答,但是在用pcntl的时候,遇到了另一个问题
http://bbs.csdn.net/topics/390829010