亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

PHP實現給用戶發微信消息提醒功能

瀏覽:3日期:2022-06-14 11:00:17
目錄正文1. 獲取access_token2. 判斷是否關注3. 發送客服消息4. 微信接口返回的是一個數組5. 實現效果正文

以前有一個項目項目,當有用戶有資金到賬或者成員變動時需要給他發一條微信消息提示。針對這個,開始想使用模板消息,但是剛注冊的公眾號申請消息模板需要幾天時間申請,在時間不足下選擇了使用客服消息接口。

這里跳過網頁授權和用戶信息獲取,請求接口的步驟,主要看獲取access_token,發布客服消息,驗證是否關注等等接口。

1. 獲取access_token// 獲取access_tokenpublic function getAccessToken($weid) {$appID = 'wxfaddfdfdfd6cf6fc3569'; // 服務號appID$appSecret = '071bebfdfdofdfd23687bf53d63a'; // 服務號appSerect$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appID&secret=$appSecret';$content = ihttp_get($url); // 自定義請求函數if(is_error($content)) { return error('-1', '獲取微信公眾號授權失敗, 請稍后重試!錯誤詳情: ' . $content['message']);}if (empty($content['content'])) { return error('-1', 'AccessToken獲取失敗,請檢查appid和appsecret的值是否與微信公眾平臺一致!');}$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', '獲取微信公眾號授權失敗, 請稍后重試! 公眾平臺返回原始數據為: 錯誤代碼-' . $errorinfo['errcode'] . ',錯誤信息-' . $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. 判斷是否關注// 判斷當前用戶是否關注公眾號public public function isSubscribe($weid,$userid) {// 獲取當前用戶信息$userinfo = pdo_get('hcface_users',array('uid'=>$userid));//return $userinfo;if(empty($userinfo)) { return false;}// 獲取access_token$accessToken = $this->getAccessToken($weid);// 是否關注接口$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 = []; // 判斷當前用戶頭像和昵稱是否更換 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. 發送客服消息public function solPushMsg($openid, $content, $wid) {// 獲取access_token$accessToken = $this->getAccessToken($wid);$data = array( 'touser' => $openid, // 用戶openID 'msgtype' => 'text', 'text' => ['content' => $content, // 內容 ],);$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第二個參數必須帶上,不然發出的消息可能是unicode編碼的if(is_error($res)) { return false;}if($res['code'] != '200') { return false;}return @json_decode($res['content'],true); }4. 微信接口返回的是一個數組

5. 實現效果

以上就是PHP實現給用戶發微信消息提醒功能的詳細內容,更多關于PHP微信消息提醒的資料請關注好吧啦網其它相關文章!

標簽: PHP
主站蜘蛛池模板: 毛片xxxx | 成年片美女福利视频在线 | 高清一级做a爱过程免费视频 | 一级片网 | 丝袜网站在线观看 | 国产在线一区二区三区四区 | 成人全黄三级视频在线观看 | 美女一级毛片免费观看 | 91久久精品一区二区 | 久久er热这里只有精品免费 | 深夜成人性视频免费看 | 日本美女黄色一级片 | 激情五月黄色 | 黄色免费网站在线播放 | 亚洲不卡视频在线观看 | 天堂久久久久va久久久久 | 日本aaaa级毛片在线看 | 全部免费国产潢色一级 | 青青青国产精品国产精品久久久久 | 免费超爽大片黄网站 | 99久久精品免费看国产一区二区 | 久久视频这里只精品3国产 久久视热这只是精品222 | 欧美中文字幕 | 国产高清自偷自在线观看 | 91久久夜色精品国产网站 | 看片在线观看免费 | 爽爽爽爽爽爽a成人免费视频 | 高清国产一区二区三区 | 免费又黄又硬又大爽日本 | 婷婷黄色| 亚洲精品美女在线观看播放 | 青青免费视频精品一区二区 | 欧美三级观看 | 痴情的后裔韩剧免费观看中文高清版 | 草逼免费看 | 特黄黄三级视频在线观看 | 久久精品黄色 | 日韩二三区 | 亚洲三级网址 | 国产成人夜色影视视频 | 久久人人青草97香蕉 |