最近要做一個(gè)微信平臺(tái)的投票活動(dòng),需要在關(guān)注公眾號(hào)之后才能參與投票,那么,如何判斷用戶(hù)是否關(guān)注了公眾號(hào)呢?
第一想法是,通過(guò)獲取公眾號(hào)的關(guān)注列表,然后搜索列表中是否有參與者的openid。
但是馬上發(fā)現(xiàn)一個(gè)問(wèn)題,就是這種方法需要每次都要獲取一下關(guān)注列表,而且,當(dāng)公眾號(hào)的粉絲比較多時(shí),這種方法就比較吃力了。
下面使用php方法,判斷用戶(hù)是否關(guān)注了公眾號(hào):
<?php
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=XXXXXXXXXXXXXXXXXX&secret=XXXXXXXXXXXXXXXXXXXXXXXXXX";
$access_msg = json_decode(file_get_contents($access_token));
$token = $access_msg->access_token;
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$_GET[openid]";
$subscribe = json_decode(file_get_contents($subscribe_msg));
$gzxx = $subscribe->subscribe;
//
if($gzxx === 1){
echo "已關(guān)注";
}else{
echo "未關(guān)注";
}
下面是第二個(gè)代碼案例:
< ? 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'未關(guān)注!';
}
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發(fā)生錯(cuò)誤:錯(cuò)誤代碼'.$json - > errcode.',微信返回錯(cuò)誤信息:'.$json - > errmsg);
}
return $json - > access_token;
}
? >
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助