公眾號判斷使用者是否已關注php代碼解析,公眾php
現在的活動,很多都引導使用者關注公眾號,才能參與到活動當中,那如何才能判斷使用者關注了公眾號呢? 本文就為大家提供php代碼,解決問題。
官方介面說明
擷取使用者基本資料(包括UnionID機制)
http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html
1、只要有基礎的access_token和使用者openid就可以判斷使用者是否關注該公眾號
2、利用的介面url為:https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid
3、判斷介面返回的欄位subscribe是否為1.【1關注,0未關注】
註:
1、判斷使用者登入的方式為靜默授權,使用者無感知,從而得到使用者的openid;
2、判斷使用者登入,需要認證服務號的支援,訂閱號不行;
下面是代碼案例
< ? php$access_token = $this - > _getAccessToken();$subscribe_msg = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$_SESSION['wecha_id'];$subscribe = json_decode($this - > curlGet($subscribe_msg));$zyxx = $subscribe - > subscribe;if ($zyxx !== 1) { echo'未關注!';}private function _getAccessToken() { $where = array('token' = > $this - > token); $this - > thisWxUser = M('Wxuser') - > where($where) - > find(); $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this - > thisWxUser['appid'].'&secret='.$this - > thisWxUser['appsecret']; $json = json_decode($this - > curlGet($url_get)); if (!$json - > errmsg) { } else { $this - > error('擷取access_token發生錯誤:錯誤碼'.$json - > errcode.',返回錯誤資訊:'.$json - > errmsg); } return $json - > access_token;}? >
以上就是本文的全部內容,希望對大家的學習有所協助。
http://www.bkjia.com/PHPjc/1138987.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138987.htmlTechArticle公眾號判斷使用者是否已關注php代碼解析,公眾php 現在的活動,很多都引導使用者關注公眾號,才能參與到活動當中,那如何才能判斷使用者...