av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP實(shí)現(xiàn)給用戶(hù)發(fā)微信消息提醒功能

瀏覽:5日期:2022-06-14 11:00:17
目錄正文1. 獲取access_token2. 判斷是否關(guān)注3. 發(fā)送客服消息4. 微信接口返回的是一個(gè)數(shù)組5. 實(shí)現(xiàn)效果正文

以前有一個(gè)項(xiàng)目項(xiàng)目,當(dāng)有用戶(hù)有資金到賬或者成員變動(dòng)時(shí)需要給他發(fā)一條微信消息提示。針對(duì)這個(gè),開(kāi)始想使用模板消息,但是剛注冊(cè)的公眾號(hào)申請(qǐng)消息模板需要幾天時(shí)間申請(qǐng),在時(shí)間不足下選擇了使用客服消息接口。

這里跳過(guò)網(wǎng)頁(yè)授權(quán)和用戶(hù)信息獲取,請(qǐng)求接口的步驟,主要看獲取access_token,發(fā)布客服消息,驗(yàn)證是否關(guān)注等等接口。

1. 獲取access_token// 獲取access_tokenpublic function getAccessToken($weid) {$appID = 'wxfaddfdfdfd6cf6fc3569'; // 服務(wù)號(hào)appID$appSecret = '071bebfdfdofdfd23687bf53d63a'; // 服務(wù)號(hào)appSerect$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appID&secret=$appSecret';$content = ihttp_get($url); // 自定義請(qǐng)求函數(shù)if(is_error($content)) { return error('-1', '獲取微信公眾號(hào)授權(quán)失敗, 請(qǐng)稍后重試!錯(cuò)誤詳情: ' . $content['message']);}if (empty($content['content'])) { return error('-1', 'AccessToken獲取失敗,請(qǐng)檢查appid和appsecret的值是否與微信公眾平臺(tái)一致!');}$token = @json_decode($content['content'], true);if ($token['errcode'] == '40164') { return error(-1, $this->errorCode($token['errcode'], $token['errmsg']));}if(empty($token) || !is_array($token) || empty($token['access_token']) || empty($token['expires_in'])) { $errorinfo = substr($content['meta'], strpos($content['meta'], '{')); $errorinfo = @json_decode($errorinfo, true); return error('-1', '獲取微信公眾號(hào)授權(quán)失敗, 請(qǐng)稍后重試! 公眾平臺(tái)返回原始數(shù)據(jù)為: 錯(cuò)誤代碼-' . $errorinfo['errcode'] . ',錯(cuò)誤信息-' . $errorinfo['errmsg']);}$record = array();$record['token'] = $token['access_token'];$record['expire'] = TIMESTAMP + $token['expires_in'] - 200;$cachekey = cache_system_key('accesstoken', array('acid' => $weid));cache_write($cachekey, $record);return $record['token']; }2. 判斷是否關(guān)注// 判斷當(dāng)前用戶(hù)是否關(guān)注公眾號(hào)public public function isSubscribe($weid,$userid) {// 獲取當(dāng)前用戶(hù)信息$userinfo = pdo_get('hcface_users',array('uid'=>$userid));//return $userinfo;if(empty($userinfo)) { return false;}// 獲取access_token$accessToken = $this->getAccessToken($weid);// 是否關(guān)注接口$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$accessToken.'&openid='.$userinfo['openid'].'&lang=zh_CN';$res = ihttp_request($url);if(is_error($res)) { return false;}if($res['code'] != '200') { return false;}$result = @json_decode($res['content'],true);if($result['subscribe'] == 1) { $updateData = []; // 判斷當(dāng)前用戶(hù)頭像和昵稱(chēng)是否更換 if($userinfo['avatar'] != $result['headimgurl']) {$updateData['avatar'] = $result['headimgurl']; } if($userinfo['nickname'] != $result['nickname']) {$updateData['avatar'] = $result['nickname']; } if(!empty($updateData)) {pdo_update('hcface_users',$updateData,array('uid'=>$userid)); }}$userInfoData = [ 'subscribe' => $result['subscribe'], 'user_openid' => $userinfo['openid'], 'nickname' => $userinfo['nickname'],];return $userInfoData; }3. 發(fā)送客服消息public function solPushMsg($openid, $content, $wid) {// 獲取access_token$accessToken = $this->getAccessToken($wid);$data = array( 'touser' => $openid, // 用戶(hù)openID 'msgtype' => 'text', 'text' => ['content' => $content, // 內(nèi)容 ],);$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$accessToken; $res = ihttp_request($url,json_encode($data,JSON_UNESCAPED_UNICODE)); // json_encode第二個(gè)參數(shù)必須帶上,不然發(fā)出的消息可能是unicode編碼的if(is_error($res)) { return false;}if($res['code'] != '200') { return false;}return @json_decode($res['content'],true); }4. 微信接口返回的是一個(gè)數(shù)組

5. 實(shí)現(xiàn)效果

以上就是PHP實(shí)現(xiàn)給用戶(hù)發(fā)微信消息提醒功能的詳細(xì)內(nèi)容,更多關(guān)于PHP微信消息提醒的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: PHP
主站蜘蛛池模板: 欧美综合久久 | 免费看欧美一级片 | 国外激情av | 欧美一二三区 | 91精品在线播放 | 国内精品视频在线观看 | 国产成人在线看 | 69视频在线播放 | 亚洲美乳中文字幕 | 亚洲大片一区 | 日韩在线观看中文字幕 | 一二区电影| 日一区二区三区 | 亚洲精品黄色 | 国产日韩一区 | 亚洲 欧美 日韩 精品 | 欧美一区二区三区国产 | 一区不卡在线观看 | 国产高潮好爽受不了了夜色 | 国产精品久久久久久亚洲调教 | 爱爱视频网 | 精品免费国产视频 | 激情一区二区三区 | 国产精品国产a级 | 日韩一区二区在线看 | www久| 日本天堂一区 | 欧美日韩一区二区电影 | 免费1区2区3区 | 在线色网| av性色全交蜜桃成熟时 | 99re在线视频免费观看 | 久久国产精品视频 | 久久久久久久夜 | 美女国产精品 | 超碰97人人人人人蜜桃 | 三级黄色片在线播放 | 麻豆久久久久久久久久 | 日本高清不卡视频 | 中文字幕在线不卡播放 | 欧美视频免费 |