微信公众号判断用户是否已关注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.kubiji.cn/topic-id3162.html

猜你喜欢

转载自scutephp.iteye.com/blog/2353566