微信公众号判断用户是否已关注php代码解析

现在的活动,很多都引导用户关注公众号,才能参与到活动当中,那如何才能判断用户关注了公众号呢? 本文就为大家提供php代码,解决问题。

官方接口说明
获取用户基本信息(包括UnionID机制) 


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; } ? >

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d3bc676c214c93967d8c5e177eabdfa1.html