极光推送

<?php
class client
{
    private $appKey;
    private $masterSecret;
    private $retryTimes;
    private $logFile;
    private static $zones = [
            'push' => 'https://api.jpush.cn/v3/',
            'report' => 'https://report.jpush.cn/v3/',
            'device' => 'https://device.jpush.cn/v3/devices/',
            'alias' => 'https://device.jpush.cn/v3/aliases/',
            'tag' => 'https://device.jpush.cn/v3/tags/',
            'schedule' => 'https://api.jpush.cn/v3/schedules/'
    ];

    public function __construct($appKey,$masterSecret)
    {
        $this->appKey = $appKey;
        $this->masterSecret = $masterSecret;
    }
    public function push() { return new PushPayload($this); }
    public function getAuthStr() { return $this->appKey . ":" . $this->masterSecret; }
    public function makeURL($key) {
        return self::$zones[$key];
    }

    public function push_res_msg($res_arr)
    {
        if(isset($res_arr['error'])){                       //如果返回了error则证明失败        
            $error_code=$res_arr['error']['code'];             //错误码
            switch ($error_code) {
                case 200:
                    $message= ['error'=>1,'msg'=>'发送成功!'];
                    break;
                case 1000:
                    $message= ['error'=>1,'msg'=>'失败(系统内部错误)'];
                    break;
                case 1001:
                    $message =['error'=>1,'msg'=>'失败(只支持 HTTP Post 方法,不支持 Get 方法)'] ;
                    break;
                case 1002:
                    $message= ['error'=>1,'msg'=>'失败(缺少了必须的参数)'];
                    break;
                case 1003:
                    $message= ['error'=>1,'msg'=>'失败(参数值不合法)'];
                    break;
                case 1004:
                    $message= ['error'=>1,'msg'=>'失败(验证失败)'];
                    break;
                case 1005:
                    $message= ['error'=>1,'msg'=>'失败(消息体太大)'] ;
                    break;
                case 1008:
                    $message= ['error'=>1,'msg'=>'失败(appkey参数非法)'];
                    break;
                case 1020:
                    $message= ['error'=>1,'msg'=>'失败(只支持 HTTPS 请求)'];
                    break;
                case 1030:
                    $message= ['error'=>1,'msg'=>'失败(内部服务超时)'] ;
                    break;
                default:
                    $message= ['error'=>1,'msg'=>'失败(返回其他状态,目前不清楚额,请联系开发人员!)'];
                    break;
            }
        }else{
            $message=['error'=>0,'msg'=>"发送成功!"];
        }
        return $message;
    }

}

/**
* 
*/
class PushPayload
{

    private $client;
    private $url;
    private $message;
    private $notification;
    private $options;
    function __construct($client)
    {
        $this->client = $client;
        $this->url = $this->client->makeURL('push').'push';
    }

    public function platform($platform)
    {
         return $this->updateAudience('platform', $platform, 'platform');
    }

    public function audience($audience)
    {
         return $this->updateAudience('audience', $audience, 'audience');
    }

     public function options($m_time)
    {
        if($m_time){
            $options = array();
            $options["sendno"]=time();
            $options["time_to_live"]=$m_time; //保存离线时间的秒数默认为一天
            $options["apns_production"]=false;
            $this->options = $options;
        }
        return $this;
    }

    public function build()
    {
        $payload = array();
        if (is_null($this->platform)) {
            return ("platform must be set");
        }
        $payload["platform"] = $this->platform;

        if (is_null($this->audience)) {
            return ("audience must be set");
        }
        $payload["audience"] = $this->audience;

         if (is_null($this->notification)) {
            return ("notification must be set");
        }
        $payload["notification"] = $this->notification;

         if (is_null($this->notification)) {
            return ("message must be set");
        }
        $payload["message"] = $this->message;

         if (is_null($this->options)) {
            return ("options must be set");
        }
        $payload["options"] = $this->options;
        return $payload;
    }

     public function send()
    {
        return Http::post($this->client, $this->url, $this->build());  
    }

    public function notification($content,array $msg = array())
    {
            $notification = array();
            if(is_string($content)){
                $notification['alert'] = $content;
                //安卓自定义
                if(!empty($msg)){
                    $notification["android"]=array(
                        "alert"=>$content,
                        "title"=>"",
                        "builder_id"=>1,
                        "extras"=>array("type"=>$msg['type'], "txt"=>$msg['txt'])
                    );
                    //ios的自定义
                    $notification["ios"]=array(
                        "alert"=>$content,
                        "badge"=>"1",
                        "sound"=>"default",
                        "extras"=>array("type"=>$msg['type'], "txt"=>$msg['txt'])
                    );
                }
                $this->notification = $notification;
            }
            return $this;
    }



    public function message($msg_content,array $msg = array())
    {
        if(is_string($msg_content)){
            $message = array();
            $message['msg_content'] = $msg_content;
            if(!empty($msg)){
                $message['extras'] = array("type"=>$msg['type'], "txt"=>$msg['txt']);
            }
            $this->message = $message;
        }
        return $this;
    }

    private function updateAudience($key, $value, $name) 
    {
        if (@is_null($this->$key)) {
            $this->$key = array();
        }
        if ($value) {
           $this->$key = $value;
        } else {
            return ("Invalid $name value");
        }
        return $this;
    }


}

class http
{
    static function post ($client,$postUrl,$param)
    {   
        $base64=base64_encode($client->getAuthStr());
        $header=array("Authorization:Basic $base64","Content-Type:application/json");
        if (empty($param)) { return false; }

        $curlPost = json_encode($param);
        $ch = curl_init();                                      //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);           // 增加 HTTP Header(头)里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);                                 //运行curl
        curl_close($ch);
        return $client->push_res_msg(json_decode($data, true));
    }



}

调用

            $content = array('tag'=>array($userinfo['tag']));//标签
                    //$content = 'all';//标签
                    $alert = option('config.push_alert');
                    $extras = ['type'=>1,'txt'=>'https://mall.epaikj.com/wap/'];
                    $m_time = 600;
                    import('PushClient');
                    $client =  new client(option('config.push_app_key'),option('config.push_secret'));
                    $res = $client->push()
                            ->platform('all')
                            ->audience($content)
                            ->notification($alert,$extras)
                            ->message($alert,$extras)
                            ->options($m_time)
                            ->send();  

 

posted @ 2018-08-18 10:17  6671  阅读(469)  评论(0编辑  收藏  举报