使用个推写得一个简单的消息推送

使用个推写个一个简单的推送功能

<?php


class Getui{
    public $AppID = '';
    public $AppKey = '';
    public $MasterSecret = '';
    public $authtoken = '';
    public function __construct($config){
        $this -> AppID = $config['AppID'];
        $this -> AppKey = $config['AppKey'];
        $this -> MasterSecret = $config['MasterSecret'];
    }


    public function postJson($url,$jsondata = ''){
        $ch = curl_init();
        $headers[] = "Content-Type: application/json";
        if(!empty($this -> authtoken)){
            $headers[] = "authtoken: " . $this -> authtoken;
        }
        $params[CURLOPT_HTTPHEADER] = $headers; //自定义header
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $jsondata;
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    public function setAuthToken ($authtoken){
        $this -> authtoken = $authtoken;
    }


    public function getAuthToken(){
        $url = "https://restapi.getui.com/v1/{$this -> AppID}/auth_sign";
        $time = time()."000";
        $sign = hash('sha256', $this -> AppKey . $time . $this -> MasterSecret);
        $data = [
            'sign' => $sign,
            'timestamp' => $time,
            'appkey' => $this -> AppKey
        ];
        $jsondata = json_encode($data);

        $info = $this -> postJson($url,$jsondata);
        $arr = json_decode($info,true);
        if($arr['result'] == 'ok'){
            return $arr['auth_token'];
        }else{
            return false;
        }
    }

    public function pushSingle(){
        $url = "https://restapi.getui.com/v1/{$this -> AppID}/push_single";
        $dataJson = [
            'message' => [
                'appkey' => $this -> AppKey,
                'is_offline' => true,
                'offline_expire_time' => 3600 * 5,
                'msgtype' => 'notification',
            ],
            'notification' => [
                'style' => [
                    'type' => 0,
                    'text' => '使用PHP推送消息',
                    'title' => '推送消息的标题'
                ],
                'transmission_type' => true,
                'transmission_content' => "透传内容"
            ],
            'cid' => '52498367fa32f900af50ae71c23990fb',//接收消息的人,
            'requestid' => md5(time().rand(100,999)),
        ];

        $json = json_encode($dataJson);
        return $this -> postJson($url,$json);
    }

    public function saveListBody(){
        $url = "https://restapi.getui.com/v1/{$this -> AppID}/save_list_body";

        $dataJson = [
            'message' => [
                'appkey' => $this -> AppKey,
                'is_offline' => true,
                'offline_expire_time' => 3600 * 5,
                'msgtype' => 'notification',
            ],
            'notification' => [
                'style' => [
                    'type' => 0,
                    'text' => '使用PHP推送群消息',
                    'title' => '推送消息的标题'
                ],
                'transmission_type' => true,
                'transmission_content' => "透传内容"
            ]
        ];

        $json = json_encode($dataJson);
        return $this -> postJson($url,$json);
    }

    public function pushList(){
        $url = "https://restapi.getui.com/v1/{$this -> AppID}/push_list";

        $get = $this -> saveListBody();
        $arr = json_decode($get,true);
        $dataJson = [
            'cid' => [
                '52498367fa32f900af50ae71c23990fb'
            ],
            'taskid' => $arr['taskid']
        ];

        $json = json_encode($dataJson);
        return $this -> postJson($url,$json);
    }
}

$config = [
    'AppID' => '***',
    'AppKey' => '***',
    'MasterSecret' => '***'
];

$obj = new Getui($config);

$authtoken = $obj -> getAuthToken();

$obj -> setAuthToken($authtoken);

//print_r($obj -> pushSingle());

print_r($obj -> pushList());

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值