Overview

Namespaces

  • None
  • sinacloud
    • sae

Classes

  • SaeADPNS
  • SaeAPNS
  • SaeChannel
  • SaeDeferredJob
  • SaeKV
  • SaeMail
  • SaeMysql
  • SaeSegment
  • SaeTAdvance
  • SaeTaskQueue
  • SaeTClientV2
  • SaeTOAuthV2
  • SaeVCode
  • sinacloud\sae\Storage
  • vDisk

Exceptions

  • sinacloud\sae\StorageException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Android 应用消息推送服务
  4:  *
  5:  * <code>
  6:  * <?php
  7:  * $appid=20001;
  8:  * $token='BYfkQcUYULUB';
  9:  * $title='title';
 10:  * $msg='hello wolrd';
 11:  * $acts="[\"2,sina.Apns,sina.Apns.MainActivity\"]";
 12:  * $extra=array(
 13:  *     'handle_by_app'=>'0'
 14:  *  );
 15:  * $adpns = new SaeADPNS();
 16:  * $result = $adpns->push($appid, $token, $title, $msg, $acts, $extra);
 17:  * 
 18:  * if( $result && is_array($result) ){
 19:  *     echo '发送成功!';
 20:  *     var_dump( $result );
 21:  * }
 22:  * else {
 23:  *     echo '发送失败。';
 24:  *     var_dump($apns->errno(), $apns->errmsg());
 25:  * }
 26:  * ?>
 27:  * </code>
 28:  *
 29:  * 错误码参考:
 30:  *  - errno: 0         成功
 31:  *  - errno: -1        信息内容为空
 32:  *  - errno: -2        连接server http请求错误
 33:  *  - errno: -3        server端错误
 34:  *
 35:  * @package sae
 36:  *
 37:  */
 38: 
 39: class SaeADPNS extends SaeObject
 40: {
 41:     private $_accesskey = "";    
 42:     private $_secretkey = "";
 43:     private $_errno = SAE_Success;
 44:     private $_errmsg = "OK";
 45: 
 46:     /**
 47:      * @ignore
 48:      */
 49:     const baseurl = "http://push.sae.sina.com.cn:81/android/android.php";
 50: 
 51:     /**
 52:      * 构造对象
 53:      *
 54:      */
 55:     function __construct() {
 56:         $this->_accesskey = SAE_ACCESSKEY;
 57:         $this->_secretkey = SAE_SECRETKEY;
 58:     }
 59: 
 60:     /**
 61:      * 推送消息
 62:      * 
 63:      * @param int $appid  SAE分配的应用序号
 64:      * @param string $token 设备令牌
 65:      * @param string $title 消息标题
 66:      * @param string $msg 消息体
 67:      * @param string $acts 跳转行为参数
 68:      *  - ["2,app包名,Activity类名"]:跳转到APP的Activity。例如:["2,com.sina.news,com.sina.news.ui.NewContentActivity"]
 69:      *  - ["4,URL地址"]:跳转到浏览器。例如:["4,http://www.sina.com"]
 70:      *  - ["5,Scheme"]:通过Scheme跳转。例如:["5,sinaweibo://sendweibo"]
 71:      * @param array  $extra 额外的传递信息 
 72:      *  - $extra = array(
 73:      *      'handle_by_app'=>'1'  // "1",表示消息交由App处理。
 74:      *    )
 75:      * @return bool 成功返回true,失败返回false
 76:      */
 77:     function push($appid, $token, $title, $msg, $acts, $extra=array()) {
 78:         $params = array();
 79:         
 80:         if (is_string($title) === false) {
 81:             $this->_errmsg = 'title must be string';
 82:             $this->_errno = -1;
 83:             return false;
 84:         }
 85:         if (is_string($msg) === false) {
 86:             $this->_errmsg = 'msg must be string';
 87:             $this->_errno = -1;
 88:             return false;
 89:         }
 90:         if (is_array($extra) === false) {
 91:             $this->_errmsg = 'extra must be array';
 92:             $this->_errno = -1;
 93:             return false;
 94:         }
 95:         
 96:         $params['act'] = "push";
 97:         $params['appid'] = intval($appid);
 98:         $params['token'] = trim($token);
 99:         $params['ak'] = $this->_accesskey;
100:         $params['title'] = trim($title);
101:         $params['msg'] = trim($msg);
102:         $params['acts'] = trim($acts);
103: 
104:         if (empty($extra) === false) {
105:             $extra = json_encode($extra);
106:             $params['extra'] = $extra;
107:         }
108: 
109:         $ret = $this->postData($params);
110:         return $ret;
111:     }
112: 
113:     /**
114:      * 取得错误码
115:      *
116:      * @return int 
117:      * @author Elmer Zhang
118:      */
119:     public function errno() {
120:         return $this->_errno;
121:     }
122: 
123:     /**
124:      * 取得错误信息
125:      *
126:      * @return string 
127:      * @author Elmer Zhang
128:      */
129:     public function errmsg() {
130:         return $this->_errmsg;
131:     }
132: 
133:     private function postData($post) {
134:         $url = self::baseurl;
135:         $s = curl_init();
136:         if (is_array($post)) {
137:             $post = http_build_query($post);
138:         }
139:         curl_setopt($s,CURLOPT_URL,$url);
140:         curl_setopt($s,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
141:         curl_setopt($s,CURLOPT_TIMEOUT,5);
142:         curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
143:         curl_setopt($s,CURLINFO_HEADER_OUT, true);
144:         curl_setopt($s,CURLOPT_POST,true);
145:         curl_setopt($s,CURLOPT_POSTFIELDS,$post); 
146:         $ret = curl_exec($s);
147:         $info = curl_getinfo($s);
148:         curl_close($s);
149: 
150:         if (empty($info['http_code'])) {
151:             $this->_errno = -2;
152:             $this->_errmsg = "can not reach push service server";
153:         } else if ($info['http_code'] != 200) {
154:             $code = $info['http_code'];
155:             $this->_errno = -2;
156:             $this->_errmsg = "httpd code: $code";
157:         } else {
158:             if ($info['size_download'] == 0) { // get MailError header
159:                 $this->_errno = -2;
160:                 $this->_errmsg = "apple push service internal error";
161:             } else {
162:                 $array = json_decode(trim($ret), true);
163:                 if (is_array($array) && is_int($array['code']) && $array['code'] < 0) {
164:                     $this->_errno = -3;
165:                     $this->_errmsg = $array['message'];
166:                     return false;
167:                 } else if (is_array($array) && is_int($array['code'])) {
168:                     $this->_errno = SAE_Success;
169:                     $this->_errmsg = 'OK';
170:                     return $array;
171:                 } else {
172:                     $this->_errno = -3;
173:                     $this->errmsg = "service response error";
174:                     return false;
175:                 }
176:             }
177:         }
178:         return false;
179:     }
180: }
181: ?>
182: 
API documentation generated by ApiGen