欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

消息推送,实时聊天

程序员文章站 2022-03-13 21:46:08
...
//消息推送
function notice_message_push($data){
//     $tag = '"all"';  发给所有用户//     $tag = '{"tag":["'.$uid.'"]}';   发给部分用户
//     $data = '{"platform": "all","audience" :'.$tag.',"notification":{"android" : {"alert" : "'.$content.'", "title":"'.$title.'","sound":"default", "badge":"+1","extras":{"newsid":321}},"ios":{"alert":"'.$content.'","title":"'.$title.'","sound":"default","badge":"+1","extras":{"newsid":321}}},"options":{"time_to_live":60,"apns_production":false}}';
    $url= "https://api.jpush.cn/v3/push";
// 启动一个CURL会话
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic MWI0MTc3NTMxODAwMTE5MjJjMzU2MjAxOjkwMjBiZjNhNGUzNzkyNTM2YjNjYzJkNw=='
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 执行操作
$response_body = curl_exec($curl);
//捕抓异常
$error_msg = "";
if (curl_errno($curl)) {
$error_msg = 'Errno' . curl_error($curl);
    }
// 关闭CURL会话
curl_close($curl);
// 返回结果
$response["response_body"] = $response_body;
$response["error_msg"] = $error_msg;
return $response;
}

以上就介绍了消息推送,实时聊天,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。