久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:2日期: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)有用戶有資金到賬或者成員變動(dòng)時(shí)需要給他發(fā)一條微信消息提示。針對(duì)這個(gè),開始想使用模板消息,但是剛注冊(cè)的公眾號(hào)申請(qǐng)消息模板需要幾天時(shí)間申請(qǐng),在時(shí)間不足下選擇了使用客服消息接口。

這里跳過網(wǎng)頁授權(quán)和用戶信息獲取,請(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)前用戶是否關(guān)注公眾號(hào)public public function isSubscribe($weid,$userid) {// 獲取當(dāng)前用戶信息$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)前用戶頭像和昵稱是否更換 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, // 用戶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)給用戶發(fā)微信消息提醒功能的詳細(xì)內(nèi)容,更多關(guān)于PHP微信消息提醒的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: PHP
主站蜘蛛池模板: 国产成人精品一区二区三区视频 | 男人天堂视频在线观看 | 在线伊人网 | 国产精品成人3p一区二区三区 | 精品人成| 日韩一区在线视频 | 亚洲一区久久久 | 欧美日韩亚洲二区 | 中文在线一区二区 | 91精品国产综合久久福利 | 久久这里只有精品首页 | 一区二区日韩精品 | 亚洲专区欧美 | 成人福利在线观看 | 久久久久久久久99精品 | 伊人激情影院 | 国产精品久久久久久久久久东京 | 女同久久| 国产区福利 | 成人国产 | 国产精品久久久久久久久久久新郎 | 久久久久久久久一区二区三区 | 欧美一级在线播放 | 国产91网址 | 我要看免费黄色片 | 欧美精品99 | 青青草精品 | 男人天堂视频网 | 亚洲一区精品在线 | 久久久资源 | 成人在线视频一区二区 | 亚洲人免费| 成人在线国产 | 最近的中文字幕在线看视频 | 亚洲精品在线视频 | 久久久999精品视频 欧美老妇交乱视频 | 国产免费一区二区 | 精品国产一区二区三区小蝌蚪 | 九色在线观看 | 91免费在线看 | av一区二区三区四区 |