知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >
微信公眾號(hào)是否關(guān)注及發(fā)送模板消息
發(fā)表時(shí)間:2020-10-29
發(fā)布人:葵宇科技
瀏覽次數(shù):57
最近遇到一個(gè)項(xiàng)目是關(guān)于微信公眾號(hào)開(kāi)發(fā)的。
公眾號(hào)連接里點(diǎn)菜單,生成一個(gè)帶二維碼的專(zhuān)屬海報(bào),分享海報(bào)到好友或者朋友圈讓好點(diǎn)點(diǎn)擊,好友關(guān)注后算是助力成功,集夠相應(yīng)好友數(shù)獲得獎(jiǎng)勵(lì)。
每一次好友助力,分享者都會(huì)收到消息,如果助力者之前沒(méi)關(guān)注過(guò)公眾號(hào)提示成功助力一人,關(guān)注過(guò)提醒助失敗。
助力者也可以生成專(zhuān)屬海報(bào),形成閉環(huán)。
開(kāi)發(fā)過(guò)程中主要用到對(duì)授權(quán)用戶(hù)進(jìn)行判斷,用戶(hù)是否關(guān)注過(guò)公眾號(hào),以及微信公眾號(hào)消息接口。
下面記錄下具體實(shí)現(xiàn)的代碼。
<?php
/*
* 模板消息發(fā)送,電腦端測(cè)試時(shí)需要手動(dòng)填寫(xiě)openid
* 微信端會(huì)自動(dòng)獲取當(dāng)前openid發(fā)送無(wú)需填寫(xiě)
*/
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$temp = new template();
//判斷用戶(hù)是否關(guān)注公眾號(hào)
function issub($openid){
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
$access_msg = json_decode(file_get_contents($access_token));
$token = $access_msg->access_token;
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid";
$subscribe = json_decode(file_get_contents($subscribe_msg));
$gzxx = $subscribe->subscribe;
//
if($gzxx === 1){
echo "已關(guān)注";
}else{
echo "未關(guān)注請(qǐng)關(guān)注。<img src='/public/images/gzh.png' />";
}
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
if (!empty($_GET['code'])) {
$openid = $temp->GetOpenid();
issub($openid);
if ($openid) {
$result = $temp->sendMsg($openid);
if ($result['errmsg'] == 'ok') {
echo '<script>alert("發(fā)送成功!");</script>';
} else {
echo '發(fā)送失敗!';
}
} else {
echo '<script>alert("獲取openID失敗!");</script>';
}
} else {
$temp->GET_Code();
}
} else {
echo '<script>alert("發(fā)送模板需要先獲取openID!");</script>';
$openid='orpdAwprTZvFaUwPDfW4WSanlZIk';
$result = $temp->sendMsg($openid);
if ($result['errmsg'] == 'ok') {
echo '<script>alert("發(fā)送成功!");</script>';
} else {
echo '發(fā)送失敗!' . $result['errmsg'];
}
}
class template {
public $appid = "APPID";
public $secret = "SECRET";
public function sendMsg($openid) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret;
//獲取access_token
$resultData = $this->https_getRequest($url);
$access_token = $resultData["access_token"];
//$openid = 'orpdAwprTZvFaUwPDfW4WSanlZIk';
$templateid = '模板ID';
$title = "標(biāo)題";
$data1 = "變量一";
$data2 = "變量二";
$data3 = "變量三";
$data4 = date("Y年m月d日 H:i", time());
$remark = "內(nèi)容說(shuō)明!";
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
/*
* @miniprogram 跳小程序所需數(shù)據(jù),不需跳小程序可不用傳該數(shù)據(jù)
* @appid appid必須與發(fā)模板消息的公眾號(hào)是綁定關(guān)聯(lián)關(guān)系)
* @pagepath 所需跳轉(zhuǎn)到小程序的具體頁(yè)面路徑,支持帶參數(shù),(示例index?foo=bar)
*/
$data = '{
"touser":"' . $openid . '",
"template_id":"' . $templateid . '",
"url":"模板跳轉(zhuǎn)網(wǎng)址",
"miniprogram":{
"appid":"",
"pagepath":""
},
"data":{
"first": {
"value":"' . $title . '",
"color":"#7A378B"
},
"keyword1":{
"value":"' . $data1 . '",
"color":"#787878"
},
"keyword2": {
"value":"' . $data2 . '",
"color":"#787878"
},
"keyword3": {
"value":"' . $data3 . '",
"color":"#787878"
},
"keyword4": {
"value":"' . $data4 . '",
"color":"#787878"
},
"remark":{
"value":"' . $remark . '",
"color":"#C5C1AA"
}
}
}';
$send = $this->https_request($url, $data);
$send = json_decode($send, true);
return $send;
}
//發(fā)起獲得code值鏈接
public function GET_Code() {
$get_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid;
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header("location:" . $get_url . "&redirect_uri=" . $url . "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
}
//獲取用戶(hù)openid
public function GetOpenid() {
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
$json_obj = $this->https_getRequest($get_token_url);
// $access_token = $json_obj['access_token'];
return $json_obj['openid'];
}
/*
* 獲取數(shù)據(jù)
*/
public function https_getRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsonData = json_decode($output, true);
return $jsonData;
}
//通用數(shù)據(jù)處理方法發(fā)送模板消息
public function https_request($url, $data = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
?>
其它配制詳見(jiàn)微信公眾平臺(tái)開(kāi)發(fā)者文檔呢。
相關(guān)案例查看更多
相關(guān)閱讀
- 生成海報(bào)
- 云南小程序開(kāi)發(fā)首選品牌
- 搜索引擎自然排名
- 霸屏推廣
- 出入小程序
- 花農(nóng)小程序
- 昆明軟件公司
- 云南網(wǎng)站建設(shè)首選
- 網(wǎng)站建設(shè)服務(wù)公司
- 云南網(wǎng)站建設(shè)價(jià)格
- 專(zhuān)業(yè)網(wǎng)站建設(shè)公司
- 楚雄小程序開(kāi)發(fā)
- 快排推廣
- 全國(guó)前十名小程序開(kāi)發(fā)公司
- 前端開(kāi)發(fā)
- 英文網(wǎng)站建設(shè)公司
- 網(wǎng)站建設(shè)首頁(yè)
- 百度小程序公司
- web前端
- 紅河小程序開(kāi)發(fā)
- 小程序開(kāi)發(fā)費(fèi)用
- 小程序開(kāi)發(fā)排名前十名
- 網(wǎng)站建設(shè)首選公司
- 商標(biāo)注冊(cè)
- 云南軟件定制公司
- 微分銷(xiāo)
- 網(wǎng)站建設(shè)高手
- 百度推廣
- 網(wǎng)站建設(shè)靠譜公司
- 云南手機(jī)網(wǎng)站建設(shè)