php APNS苹果推送通知服务的服务器端公共类
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-15 11:38:09 浏览: 评论:0
APNS(英文全称:Apple Push Notification Service),中文翻译为:苹果推送通知服务,[1] 该技术由苹果公司提供的APNS服务,下面来看一个比较完善的苹果推送通知服务的php服务器端公共类.
前段时间开发的一套APNS推送平台效率很差,通过再次深入研究苹果的消息推送服务,总结了不少经验,同时也参考了网上一些技术blog的博文,重新完善了此前写过的一个PHP类,代码如下:
- <?php
- /**
- * ApplePush 苹果消息推送公共类
- */
- class ApplePush
- {
- const STATUS_CODE_INTERNAL_ERROR = 999;
- const ERROR_RESPONSE_SIZE = 6;
- const ERROR_RESPONSE_COMMAND = 8;
- protected $_errorResponseMessages = array(
- 0 => 'No errors encountered',
- 1 => 'Processing error',
- 2 => 'Missing device token',
- 3 => 'Missing topic',
- 4 => 'Missing payload',
- 5 => 'Invalid token size',
- 6 => 'Invalid topic size',
- 7 => 'Invalid payload size',
- 8 => 'Invalid token',
- self::STATUS_CODE_INTERNAL_ERROR => 'Internal error'
- );
- /**
- * APNS server url
- *
- * @var string
- */
- protected $apns_url = 'ssl://gateway.push.apple.com:2195'; //沙盒地址:ssl://gateway.sandbox.push.apple.com:2195
- /**
- * 推送数据
- *
- * @var string
- */
- private $payload_json;
- /**
- * 数据流对象
- *
- * @var mixed
- */
- private $fp;
- /**
- * 设置APNS地址
- *
- * @param string $url
- */
- public function setApnsUrl($url)
- {
- if (emptyempty($url)) {
- return false;
- } else {
- $this->apns_url = $url;
- }
- return true;
- }
- /**
- * 设置推送的消息
- *
- * @param string $body
- */
- public function setBody($body)
- {
- if (emptyempty($body)) {
- return false;
- } else {
- $this->payload_json = json_encode($body);
- }
- return true;
- }
- /**
- * Open 打开APNS服务器连接
- *
- * @param string $pem 证书
- * @param string $passphrase 证书密钥
- */
- public function open($pem, $passphrase)
- {
- if (emptyempty($pem)) {
- return false;
- }
- if (emptyempty($passphrase)) {
- return false;
- }
- $ctx = stream_context_create();
- stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
- stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
- $fp = stream_socket_client($this->apns_url, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
- if (!$fp) {
- return false;
- }
- $this->fp = $fp;
- return true;
- }
- /**
- * Send 推送
- *
- * @param string $token
- */
- public function send($token, $id)
- {
- $msg = pack('CNNnH*', 1, $id, 864000, 32, $token) . pack('n', strlen($this->payload_json)) . $this->payload_json;
- // Send it to the server
- $result = fwrite($this->fp, $msg, strlen($msg));
- return $result;
- }
- public function readErrMsg()
- {
- $errInfo = @fread($this->fp, self::ERROR_RESPONSE_SIZE);
- if ($errInfo === false || strlen($errInfo) != self::ERROR_RESPONSE_SIZE) { //开源软件:phpfensi.com
- return true;
- }
- $errInfo = $this->parseErrMsg($errInfo);
- if (!is_array($errInfo) || emptyempty($errInfo)) {
- return true;
- }
- if (!isset($errInfo['command'], $errInfo['statusCode'], $errInfo['identifier'])) {
- return true;
- }
- if ($errInfo['command'] != self::ERROR_RESPONSE_COMMAND) {
- return true;
- }
- $errInfo['timeline'] = time();
- $errInfo['statusMessage'] = 'None (unknown)';
- $errInfo['errorIdentifier'] = $errInfo['identifier'];
- if (isset($this->_aErrorResponseMessages[$errInfo['statusCode']])) {
- $errInfo['statusMessage'] = $this->_errorResponseMessages[$errInfo['statusCode']];
- }
- return $errInfo;
- }
- protected function parseErrMsg($errorMessage)
- {
- return unpack('Ccommand/CstatusCode/Nidentifier', $errorMessage);
- }
- /**
- * Close APNS server 关闭APNS服务器连接
- *
- */
- public function close()
- {
- // Close the connection to the server
- fclose($this->fp);
- return true;
- }
- }
- ?>
Tags: APNS苹果推送 php推送通知
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)