Overview

Namespaces

  • None
  • sinacloud
    • sae

Classes

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

Exceptions

  • OAuthException
  • sinacloud\sae\StorageException
  • Overview
  • Namespace
  • Class
  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
/**
 * Apple 应用消息推送服务
 *
 * <code>
 * <?php
 * $cert_id = 1;
 * $device_token = "xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx";
 * 
 * $message = "测试消息";
 * $body = array(
 *     'aps' => array( 'alert' => $message , 'badge' => 1, 'sound' => 'in.caf')
 * );
 * $apns = new SaeAPNS();
 * $result = $apns->push( $cert_id , $body , $device_token );
 * 
 * if( $result && is_array($result) ){
 *     echo '发送成功!';
 *     var_dump( $result );
 * }
 * else {
 *     echo '发送失败。';
 *     var_dump($apns->errno(), $apns->errmsg());
 * }
 * ?>
 * </code>
 *
 * 错误码参考:
 *  - errno: 0         成功
 *  - errno: -1        信息内容为空
 *  - errno: -2        连接server http请求错误
 *  - errno: -3        server端错误
 *
 * @package sae
 *
 */
class SaeAPNS extends SaeObject
{
    private $_accesskey = "";    
    private $_secretkey = "";
    private $_errno = SAE_Success;
    private $_errmsg = "OK";

    /**
     * @ignore
     */
    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);
    }

    /**
     * 推送消息
     * 
     * @param int $cert_id  证书序号
     * @param array $body 消息体(包括消息、提醒声音等等),格式请参考Apple官方文档}
     * @param string $device_token 设备令牌
     * @return bool 成功返回true,失败返回false
     */
    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);
    }

    /**
     * 查看当天推送汇总信息
     * 
     * @param int $cert_id  证书序号
     * @return mix 成功json格式汇总信息,失败返回false
     */
    function getInfo($cert_id) {
        return $this->postData('apple/log/info', Array(
                  'certid' => intval($cert_id),
                  'ak'     => $this->_accesskey));
    }

    /**
     * 取得错误码
     *
     * @return int 
     * @author Elmer Zhang
     */
    public function errno() {
        return $this->_errno;
    }

    /**
     * 取得错误信息
     *
     * @return string 
     * @author Elmer Zhang
     */
    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;
    }
}
API documentation generated by ApiGen