1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 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: 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: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 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: 117: 118:
119: public function errno() {
120: return $this->_errno;
121: }
122:
123: 124: 125: 126: 127: 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) {
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: