您好,登錄后才能下訂單哦!
PHP中怎么判斷用戶是否關(guān)注該公眾號(hào),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
const _Biz = '自己的微信公眾號(hào)biz參數(shù)';//用于直接跳轉(zhuǎn)到一鍵關(guān)注公眾號(hào)頁(yè)面 protected $appid = "your appid"; protected $appsecret = "your appsecret "; public $url_wxcallback = "微信授權(quán)后的回調(diào)地址(會(huì)接收到code)"; public $url = "如果關(guān)注了需要跳轉(zhuǎn)的地址"; /** * 判斷是否關(guān)注該公眾號(hào) */ public function isGz() { $param = $_GET['id'] ?? ''; //傳的某個(gè)參數(shù) //跳轉(zhuǎn)到微信網(wǎng)頁(yè)授權(quán)頁(yè) $this->getCode($this->appid, $this->url_wxcallback, $param ,'snsapi_base'); } /** * 封裝的微信授權(quán)調(diào)用鏈接跳轉(zhuǎn) */ public function getCode($appId, $redirect_uri, $state=1, $scope='snsapi_base', $response_type='code'){ $url = 'https://open.weixin.qq.com/connect/oauth3/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type='.$response_type.'&scope='.$scope.'&state='.$state.'#wechat_redirect'; header('Location: '.$url, true, 301); } /** * 微信授權(quán)后的回調(diào)方法 */ public function callback(){ $isconcern = 0;// 默認(rèn)未關(guān)注 $code = $_GET['code'] ?? ''; $id= $_GET['state'] ?? '';//前面?zhèn)鞯?nbsp;param 參數(shù) $token = self::getToken($this->appid, $this->appsecret); $accessTokenInfo = self::getAccessToken($code, $this->appid, $this->appsecret); $openId = $accessTokenInfo['openid']; $accessToken = $accessTokenInfo['access_token']; $userInfo = self::getUserInfo($openId, $token['access_token']); if($userInfo['subscribe'] == 1){ $isconcern = 1; // 已關(guān)注 header('Location: '.$this->url.'?id='.$id, true, 301);exit(); } $isconcern = 0; //未關(guān)注 //1 展示關(guān)注二維碼圖片 長(zhǎng)按關(guān)注 echo('<h3>請(qǐng)長(zhǎng)按圖片關(guān)注該公眾號(hào)</h3>'); echo "<img src='/static/img/gzh_qrcode.jpg'>"; exit(); //2 跳轉(zhuǎn)到一鍵關(guān)注公眾號(hào)頁(yè)面(棄用:因?yàn)榉窃谥苯釉谖⑿糯蜷_(kāi)此鏈接的 該頁(yè)面沒(méi)有關(guān)注按鈕) //$gzh_url = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=".self::_Biz."&scene=110#wechat_redirect"; //header('Location: '.$gzh_url); } static function getToken($appid, $appsecret){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; return self::geturl($url); } static function getAccessToken($code, $appid, $appsecret, $grant_type='authorization_code'){ $url = 'https://api.weixin.qq.com/sns/oauth3/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type='.$grant_type.''; return self::geturl($url); } static function getUserInfo($openId, $token){ $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$openId.'&lang=zh_CN'; return self::geturl($url); } /* * 封裝的curl請(qǐng)求方法 */ static function geturl($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true); return $output; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。