現在的活動,很多都引導用戶關注公眾號,才能參與到活動當中,那如何才能判斷用戶關注了公眾號呢? 本文就為大家提供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;
}
? >
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。