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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-22 我要评论
想了解php判断用户是否关注微信公众号的相关内容吗,在本文为您仔细讲解php关注微信公众号的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php判断关注公众号,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;
}
? >

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们