search

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 />}

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

------解决方案--------------------
这种东西肯定需要用异步去实现
------解决方案--------------------
试试xmpphp,使用聊天服务器来实现,可能更靠谱一些。
------解决方案--------------------
要快就只能開多進程了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)