1: 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: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
<?php
class SaeAPNS extends SaeObject
{
private $_accesskey = "";
private $_secretkey = "";
private $_errno = SAE_Success;
private $_errmsg = "OK";
const baseurl = "http://push.sae.sina.com.cn:81";
function __construct() {
$this->_accesskey = SAE_ACCESSKEY;
$this->_secretkey = SAE_SECRETKEY;
$this->request = new SaeRequest(self::baseurl);
}
function push($cert_id, $body, $device_token) {
if(!is_array($body) || !isset($body['aps']['alert'])){
$this->_errmsg = 'body must be an array';
$this->_errno = -1;
return false;
}
$params = array();
$params['certid'] = intval($cert_id);
$params['token'] = trim($device_token);
$params['ak'] = $this->_accesskey;
$post = array();
$encodings = array( 'UTF-8', 'GBK', 'BIG5' );
if (is_string($body['aps']['alert'])) {
$charset = mb_detect_encoding( $body['aps']['alert'] , $encodings);
if ( $charset !='UTF-8' ) {
$body['aps']['alert'] = mb_convert_encoding( $body['aps']['alert'], "UTF-8", $charset);
}
} else if (is_array($body['aps']['alert'])) {
if (isset($body['aps']['alert']['body'])) {
$charset = mb_detect_encoding( $body['aps']['alert']['body'] , $encodings);
if ( $charset !='UTF-8' ) {
$body['aps']['alert']['body'] = mb_convert_encoding( $body['aps']['alert']['body'], "UTF-8", $charset);
}
}
}
$post['body'] = json_encode($body);
return $this->postData("apple/message/push", $params, $post);
}
function getInfo($cert_id) {
return $this->postData('apple/log/info', Array(
'certid' => intval($cert_id),
'ak' => $this->_accesskey));
}
public function errno() {
return $this->_errno;
}
public function errmsg() {
return $this->_errmsg;
}
private function postData($path, $args=null, $body=null) {
try {
$this->request->post($path, $args, $body);
} catch (Exception $e) {
$this->_errmsg = $e->getMessage();
if ($e->getCode() == SaeRequest::NET_ERROR ||
$e->getCode() == SaeRequest::SERVER_ERROR) {
$this->_errno = SAE_ErrInternal;
} else {
$this->_errno = SAE_ErrUnknown;
}
return False;
}
return True;
}
}