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

发布时间:2020-12-16 16:31:08 作者:Leah
来源:亿速云 阅读:308

这篇文章将为大家详细讲解有关使用php怎么判断用户是否关注微信公众号,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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

第一想法是,通过获取公众号的关注列表,然后搜索列表中是否有参与者的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;
}
? >

关于使用php怎么判断用户是否关注微信公众号就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 收集关注用户 以及关注文章
  2. php判断用户是否登录的代码分享

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 微信公众

上一篇:怎么对phpMyAdmin进行安装与配置

下一篇:在PHP中使用file_exists时会遇到哪些问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》