当前位置:首页 > PHP教程 > php类库 > 列表

php APNS苹果推送通知服务的服务器端公共类

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-15 11:38:09 浏览: 评论:0 

APNS(英文全称:Apple Push Notification Service),中文翻译为:苹果推送通知服务,[1] 该技术由苹果公司提供的APNS服务,下面来看一个比较完善的苹果推送通知服务的php服务器端公共类.

前段时间开发的一套APNS推送平台效率很差,通过再次深入研究苹果的消息推送服务,总结了不少经验,同时也参考了网上一些技术blog的博文,重新完善了此前写过的一个PHP类,代码如下:

  1. <?php 
  2. /** 
  3.  * ApplePush 苹果消息推送公共类 
  4.  */ 
  5. class ApplePush 
  6. { 
  7.      
  8.     const STATUS_CODE_INTERNAL_ERROR = 999; 
  9.     const ERROR_RESPONSE_SIZE = 6; 
  10.     const ERROR_RESPONSE_COMMAND = 8; 
  11.      
  12.     protected $_errorResponseMessages = array( 
  13.         0 => 'No errors encountered', 
  14.         1 => 'Processing error', 
  15.         2 => 'Missing device token', 
  16.         3 => 'Missing topic', 
  17.         4 => 'Missing payload', 
  18.         5 => 'Invalid token size', 
  19.         6 => 'Invalid topic size', 
  20.         7 => 'Invalid payload size', 
  21.         8 => 'Invalid token', 
  22.         self::STATUS_CODE_INTERNAL_ERROR => 'Internal error' 
  23.     ); 
  24.      
  25.     /** 
  26.      * APNS server url 
  27.      *  
  28.      * @var string 
  29.      */ 
  30.     protected $apns_url = 'ssl://gateway.push.apple.com:2195'; //沙盒地址:ssl://gateway.sandbox.push.apple.com:2195 
  31.      
  32.     /** 
  33.      * 推送数据 
  34.      *  
  35.      * @var string 
  36.      */ 
  37.     private $payload_json; 
  38.      
  39.     /** 
  40.      * 数据流对象 
  41.      *  
  42.      * @var mixed 
  43.      */ 
  44.     private $fp; 
  45.      
  46.     /** 
  47.      * 设置APNS地址 
  48.      *  
  49.      * @param string $url 
  50.      */ 
  51.       
  52.     public function setApnsUrl($url) 
  53.     { 
  54.         if (emptyempty($url)) { 
  55.             return false; 
  56.         } else { 
  57.             $this->apns_url = $url; 
  58.         } 
  59.         return true; 
  60.     } 
  61.      
  62.     /** 
  63.      * 设置推送的消息 
  64.      *  
  65.      * @param string $body 
  66.      */ 
  67.     public function setBody($body) 
  68.     { 
  69.         if (emptyempty($body)) { 
  70.             return false; 
  71.         } else { 
  72.             $this->payload_json = json_encode($body); 
  73.         } 
  74.         return true; 
  75.     } 
  76.      
  77.     /** 
  78.      * Open 打开APNS服务器连接 
  79.      *  
  80.      * @param string $pem 证书 
  81.      * @param string $passphrase 证书密钥 
  82.      */ 
  83.     public function open($pem, $passphrase) 
  84.     { 
  85.         if (emptyempty($pem)) { 
  86.             return false; 
  87.         } 
  88.         if (emptyempty($passphrase)) { 
  89.             return false; 
  90.         } 
  91.         $ctx = stream_context_create(); 
  92.         stream_context_set_option($ctx, 'ssl', 'local_cert', $pem); 
  93.         stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
  94.         $fp = stream_socket_client($this->apns_url, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); 
  95.         if (!$fp) { 
  96.             return false; 
  97.         } 
  98.         $this->fp = $fp; 
  99.         return true; 
  100.     } 
  101.      
  102.     /** 
  103.      * Send 推送 
  104.      *  
  105.      * @param string $token 
  106.      */ 
  107.     public function send($token, $id) 
  108.     { 
  109.         $msg = pack('CNNnH*', 1, $id, 864000, 32, $token) . pack('n', strlen($this->payload_json)) . $this->payload_json; 
  110.         // Send it to the server 
  111.         $result = fwrite($this->fp, $msg, strlen($msg)); 
  112.         return $result; 
  113.     } 
  114.      
  115.     public function readErrMsg() 
  116.     { 
  117.         $errInfo = @fread($this->fp, self::ERROR_RESPONSE_SIZE); 
  118.         if ($errInfo === false || strlen($errInfo) != self::ERROR_RESPONSE_SIZE) {  //开源软件:phpfensi.com 
  119.             return true; 
  120.         } 
  121.         $errInfo = $this->parseErrMsg($errInfo); 
  122.         if (!is_array($errInfo) || emptyempty($errInfo)) { 
  123.             return true; 
  124.         } 
  125.         if (!isset($errInfo['command'], $errInfo['statusCode'], $errInfo['identifier'])) { 
  126.             return true; 
  127.         } 
  128.         if ($errInfo['command'] != self::ERROR_RESPONSE_COMMAND) { 
  129.             return true; 
  130.         } 
  131.         $errInfo['timeline'] = time(); 
  132.         $errInfo['statusMessage'] = 'None (unknown)'; 
  133.         $errInfo['errorIdentifier'] = $errInfo['identifier']; 
  134.         if (isset($this->_aErrorResponseMessages[$errInfo['statusCode']])) { 
  135.             $errInfo['statusMessage'] = $this->_errorResponseMessages[$errInfo['statusCode']]; 
  136.         } 
  137.         return $errInfo; 
  138.     } 
  139.     protected function parseErrMsg($errorMessage) 
  140.     { 
  141.         return unpack('Ccommand/CstatusCode/Nidentifier', $errorMessage); 
  142.     } 
  143.      
  144.     /** 
  145.      * Close APNS server 关闭APNS服务器连接 
  146.      *  
  147.      */ 
  148.     public function close() 
  149.     { 
  150.         // Close the connection to the server 
  151.         fclose($this->fp); 
  152.         return true; 
  153.     } 
  154. } 
  155. ?>

Tags: APNS苹果推送 php推送通知

分享到: