描述:用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);}
单条推送是没有问题的
问题描述:
现在要求群体推送,之前,我写的是用socket链接后,循环写入,再关闭fp,但是有些人没有收到推送,网上说,要是其中一个人的手机令牌(deviceToken)有问题的话,苹果服务器就会中断这个socket链接。
我就改成每个用户都用socket打开,写入,关闭,但是我发现这样很慢,半个小时都不能推送9000条,虽然是后台推送,但是要求劲量在半小时内推送完。
我先在想到的办法是:同时访问这个php页面十次,参数不用,就将要推送的用户分成十份。
不知道有什么好的群体推送方式
附上现在的code
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

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
