概述
使用个推写个一个简单的推送功能
<?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());
最后
以上就是聪慧手链为你收集整理的使用个推写得一个简单的消息推送的全部内容,希望文章能够帮你解决使用个推写得一个简单的消息推送所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复