第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

PHP公眾號(hào)開(kāi)發(fā)給用戶發(fā)微信消息提醒功能

標(biāo)簽:
PHP API

最近做的一个项目,当有用户有资金到账或者成员变动时需要给他发一条微信消息提示。针对这个,开始想使用模板消息,但是刚注册的公众号申请消息模板需要几天时间申请,在时间不足下选择了使用客服消息接口。

    这里跳过网页授权和用户信息获取,请求接口的步骤,主要看获取access_token,发布客服消息,验证是否关注公众号等接口。

https://img2.sycdn.imooc.com/645cb0f60001857a15640883.jpg

https://img2.sycdn.imooc.com/645cb0f60001c4ba16590916.jpg

 

1. 获取access_token。

// 获取access_token public 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. 微信接口返回的是一个数组。

https://img2.sycdn.imooc.com/645cb0f700014e8c12990588.jpg

 

5. 实现效果。

https://img4.sycdn.imooc.com/645cb0f70001786704400181.jpg



點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
PHP開(kāi)發(fā)工程師
手記
粉絲
3
獲贊與收藏
11

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消