PHP實(shí)現(xiàn)給用戶發(fā)微信消息提醒功能
正文
以前有一個項(xiàng)目項(xiàng)目,當(dāng)有用戶有資金到賬或者成員變動時需要給他發(fā)一條微信消息提示。針對這個,開始想使用模板消息,但是剛注冊的公眾號申請消息模板需要幾天時間申請,在時間不足下選擇了使用客服消息接口。
這里跳過網(wǎng)頁授權(quán)和用戶信息獲取,請求接口的步驟,主要看獲取access_token,發(fā)布客服消息,驗(yàn)證是否關(guān)注等等接口。


1. 獲取access_token
// 獲取access_token
public function getAccessToken($weid) {
$appID = "wxfaddfdfdfd6cf6fc3569"; // 服務(wù)號appID
$appSecret = "071bebfdfdofdfd23687bf53d63a"; // 服務(wù)號appSerect
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appID&secret=$appSecret";
$content = ihttp_get($url); // 自定義請求函數(shù)
if(is_error($content)) {
return error('-1', '獲取微信公眾號授權(quán)失敗, 請稍后重試!錯誤詳情: ' . $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', '獲取微信公眾號授權(quán)失敗, 請稍后重試! 公眾平臺返回原始數(shù)據(jù)為: 錯誤代碼-' . $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. 判斷是否關(guān)注
// 判斷當(dāng)前用戶是否關(guān)注公眾號
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第二個參數(shù)必須帶上,不然發(fā)出的消息可能是unicode編碼的
if(is_error($res)) {
return false;
}
if($res['code'] != '200') {
return false;
}
return @json_decode($res['content'],true);
}4. 微信接口返回的是一個數(shù)組

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

以上就是PHP實(shí)現(xiàn)給用戶發(fā)微信消息提醒功能的詳細(xì)內(nèi)容,更多關(guān)于PHP微信消息提醒的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
讓CodeIgniter數(shù)據(jù)庫緩存自動過期的處理的方法
按官方的說法,緩存設(shè)置后永不過期,除非你調(diào)用方法主動刪除。這篇文章主要介紹了CodeIgniter數(shù)據(jù)庫緩存自動過期的處理,需要的朋友可以參考下2014-06-06
使用PHP Socket 編程模擬Http post和get請求
這篇文章主要介紹了使用PHP Socket 編程模擬Http post和get請求 ,需要的朋友可以參考下2014-11-11
thinkphp5框架實(shí)現(xiàn)的自定義擴(kuò)展類操作示例
這篇文章主要介紹了thinkphp5框架實(shí)現(xiàn)的自定義擴(kuò)展類操作,結(jié)合實(shí)例形式簡單分析了thinkPHP5在extend目錄下建立自定義擴(kuò)展類的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05
laravel實(shí)現(xiàn)圖片上傳預(yù)覽,及編輯時可更換圖片,并實(shí)時變化的例子
今天小編就為大家分享一篇laravel實(shí)現(xiàn)圖片上傳預(yù)覽,及編輯時可更換圖片,并實(shí)時變化的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
thinkPHP框架對接支付寶即時到賬接口回調(diào)操作示例
這篇文章主要介紹了thinkPHP框架對接支付寶即時到賬接口回調(diào)操作,結(jié)合實(shí)例形式分析了thinkPHP針對支付寶接口回調(diào)操作的原理與具體操作步驟,需要的朋友可以參考下2016-11-11

