WordPress收到評論的時候可以通過郵件發送評論通知,但是郵件通知的可能不是那么能及時的查看,所以增加一個微信通知,
實作方式:
1、第一種就是通過企業微信群聊機器人來通知,
2、第二種就是通過server醬來實作通知
這兩種方式各有好壞,通過server醬通知的方式很多,選擇性很多,但是畢竟是用的別人的服務,涉及安全,穩定以及需要會員的問題,用自己的企業微信來通知就不需要考慮服務不可用的問題,通知頻率也不會限制,缺點是選擇性就一個,
代碼:
企業微信版本:
date_default_timezone_set("Asia/Shanghai");
// 請求體
function request_post($url = '', $post_data = https://www.cnblogs.com/lieone/p/array(),$dataType='') {
if (empty($url) || empty($post_data)) {
return false;
}
$curlPost = $post_data;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($dataType=='json'){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: ' . strlen($curlPost)
)
);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = https://www.cnblogs.com/lieone/p/curl_exec($ch);
return $data;
}
// 呼叫代碼
function commit_send($comment_id) {
$comment = get_comment($comment_id);
$url ='企業微信機器人webHook地址';
// 評論內容
$cont = $comment->comment_content;
// 評論人昵稱
$title = $comment->comment_author;
// 文本訊息
$data = https://www.cnblogs.com/lieone/p/array("msgtype"=>"text",
"text"=>array(
"content"=>$cont,
"mentioned_list"=>array("@all") // 可以@群全部成員
)
);
// 圖文訊息(二選一)
$data = https://www.cnblogs.com/lieone/p/array("msgtype"=>"news",
"news"=>array(
"articles"=>array(
array(
"title"=>date("Y-m-d H:i:s",time())."@".$title."給你發來一條評論",
"description"=>$cont,
"url"=>"https://cuixinxin.cn/about", // 跳轉地址
"picurl"=>"http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx", // 圖文訊息封面
)
)
)
);
$res = request_post($url, json_encode($data,'320'),'json');
}
add_action('comment_post', 'commit_send', 19, 2);
server醬版本:
function commit_send($comment_id) {
// server醬
$comment = get_comment($comment_id);
$text = '@'.$comment->comment_author.'發來了一條評論';
$desp = $comment->comment_content;
$key = server醬的key;
$postdata = https://www.cnblogs.com/lieone/p/http_build_query(
array('text' => $text,
'desp' => $desp
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return $result = file_get_contents('https://sctapi.ftqq.com/'.$key.'.send', false, $context);
}
add_action('comment_post', 'commit_send', 19, 2);
實作效果
企業微信注冊方式
我是賣堅果的怪叔叔—— 一枚佛系前端開發,會一丟丟攝影,喜歡折騰,愛好美食,分享點前端技巧、筆記以及各種有趣的APP和資源教程??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296121.html
標籤:其他
上一篇:基于Java RMI的網路聊天室
下一篇:Java類和物件的概念
