当前位置: 移动技术网 > IT编程>开发语言>PHP > php判断用户是否关注微信公众号

php判断用户是否关注微信公众号

2017年12月12日  | 移动技术网IT编程  | 我要评论

环境变量,追爱千里行,食品添加剂作用

最近要做一个微信平台的投票活动,需要在关注公众号之后才能参与投票,那么,如何判断用户是否关注了公众号呢?

第一想法是,通过获取公众号的关注列表,然后搜索列表中是否有参与者的openid

但是马上发现一个问题,就是这种方法需要每次都要获取一下关注列表,而且,当公众号的粉丝比较多时,这种方法就比较吃力了。

下面使用php方法,判断用户是否关注了公众号:

<?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 "已关注";
    }else{
    echo "未关注";
    
 }

下面是第二个代码案例

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网