极光推送API调用封装

文档见:http://docs.jpush.cn/display/dev/Push-API-v3

不废话,直接上代码,不懂的提问

<?php
/**
 * 测试接口
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/2
 * Time: 18:21
 */
//极光推送的类
//文档见:http://docs.jpush.cn/display/dev/Push-API-v3

/***使用示例
$pushObj = new Jpush();
//组装需要的参数
//$receive = 'all';     //全部
//$receive = array('tag'=>array('2401','2588','9527'));      //标签
$receive = array('alias'=>array('93d78b73611d886a74*****88497f501'));    //别名
$content = '这是一个测试的推送数据....测试....Hello World...';
$m_type = 'http';
$m_txt = 'http://www.iqujing.com/';
$m_time = '600';        //离线保留时间

//调用推送,并处理
$result = $pushObj->push($receive,$content,$m_type,$m_txt,$m_time);
if($result){
$res_arr = json_decode($result, true);
if(isset($res_arr['error'])){                       //如果返回了error则证明失败
echo $res_arr['error']['message'];          //错误信息
echo $res_arr['error']['code'];             //错误码
return false;
}else{
//处理成功的推送......
echo '推送成功.....';
return true;
}
}else{      //接口调用失败或无响应
echo '接口调用失败或无响应';
return false;
}
 ***/

class Jpush extends CController{

    private $app_key = 'cc2d3*****2911';            //待发送的应用程序(appKey),只能填一个。
    private $master_secret = '8c7*******20cf';        //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址
    //若实例化的时候传入相应的值则按新的相应值进行
    public function __construct($app_key=null, $master_secret=null,$url=null) {
        if ($app_key) $this->app_key = $app_key;
        if ($master_secret) $this->master_secret = $master_secret;
        if ($url) $this->url = $url;
    }

    public function push($receiver='all',$content='',$title="",$extras=array('new_id'=>'123')){
        $base64=base64_encode("$this->app_key:$this->master_secret");
        $header=array("Authorization:Basic $base64","Content-Type:application/json");
        $data = array();
        $data['platform'] = array('android','ios');          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = $receiver;      //目标用户{"registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ]}

        $data['notification'] = array(
            //统一的模式--标准模式
            "alert"=>$content,
            //安卓自定义
            "android"=>array(
                "alert"=>$content,
                "title"=>$title,
                "builder_id"=>1,
                "extras"=>$extras
            ),
            //ios的自定义
            "ios"=>array(
                "alert"=>$content,
                "badge"=>"+1",
                "sound"=>"default",
                "extras"=>$extras
            ),
        );

        //苹果自定义---为了弹出值方便调测
        $data['message'] = array(
            "msg_content"=>$content,
            "extras"=>$extras
        );

        //附加选项
        $data['options'] = array(
            "sendno"=>time(),
            "apns_production"=>1,        //指定 APNS 通知发送环境:0开发环境,1生产环境。
        );
        $param = json_encode($data);
//        echo $param;die;
        $res = $this->push_curl($param,$header);

        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{
            //未得到返回值--返回失败
            return false;
        }
    }
    public function Trigger($content='',$name,$time,$extras){
        $this->url ="https://bjapi.push.jiguang.cn/v3/push/schedules";
        $base64=base64_encode("$this->app_key:$this->master_secret");
        $header=array("Authorization:Basic $base64","Content-Type:application/json");
        $data = array();
        $data['name'] = $name;
        $data['enabled'] = true;
        $data['trigger'] = array('single'=>array("time"=>$time));

        $data['push']['platform'] = array('android','ios');          //目标用户终端手机的平台类型android,ios,winphone
        $data['push']['audience'] = 'all';      //目标用户{"registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ]} "title"=>$name,
//        $data['push']['audience'] = array("alias"=> array('10772',"15627"));
        $data['push']['notification'] = array(
            //统一的模式--标准模式
            "alert"=>array(
                "body" =>$content
            ),
            //安卓自定义
            "android"=>array(
                "alert"=>$content,
                "builder_id"=>1,
                "extras"=>$extras
            ),
            //ios的自定义
            "ios"=>array(
                "badge"=>"+1",
                "sound"=>"default",
                "extras"=>$extras
            ),
        );

        //苹果自定义---为了弹出值方便调测
        $data['push']['message'] = array(
            "msg_content"=>$content,
            "extras"=>$extras
        );

        //附加选项
        $data['push']['options'] = array(
            "sendno"=>time(),
            "apns_production"=>1,        //指定 APNS 通知发送环境:0开发环境,1生产环境。
        );
        $param = json_encode($data);
//        echo $param;die;
        $res = $this->push_curl($param,$header,'POST');

        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{
            //未得到返回值--返回失败
            return false;
        }
    }
    //获取任务列表
    public function Push_lst($page){
        $this->url = "https://api.jpush.cn/v3/schedules?page=".$page;
        $base64=base64_encode("$this->app_key:$this->master_secret");
        $header=array("Authorization:Basic $base64","Content-Type:application/json","Accept:application/json");
        $res = $this->push_curl("",$header);
        if($res){   //得到返回值--成功已否后面判断
            return $res;
        }else{
            //未得到返回值--返回失败
            return false;
        }
    }
    //删除定时任务
    public function Push_del($id){
        $this->url = "https://api.jpush.cn/v3/schedules/".$id;
        $base64=base64_encode("$this->app_key:$this->master_secret");
        $header=array("Authorization:Basic $base64","Content-Type:application/json","Accept:application/json");
        $res = $this->push_curl("",$header,'DELETE');
        if($res){   //得到返回值--成功已否后面判断
            return $res;
        }else{
            //未得到返回值--返回失败
            return false;
        }
    }
    //推送的Curl方法
    public function push_curl($param="",$header="",$method = 'GET') {
        if (!empty($param) && $method=='POST') {
            $curlPost = $param;
        }
        $postUrl = $this->url;
        $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_CUSTOMREQUEST, $method);//3.请求方式
        if($method=="POST"){//5.post方式的时候添加数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        }
//        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 $data;
    }
}

猜你喜欢

转载自blog.csdn.net/zclwjy/article/details/81699291