Determine whether the user follows the WeChat official account through PHP

PHP中文网
Release: 2023-02-28 22:56:01
Original
2571 people have browsed it

Recently, there will be a voting event on the WeChat platform. You need to follow the official account before you can participate in the voting. So, how to judge whether the user has followed the official account?

The first idea is to get the public account’s watch list and then search whether there is a participant’s openid in the list.

But I immediately discovered a problem, that is, this method requires getting the watch list every time, and when the official account has a lot of fans, this method is more difficult.

The following uses the PHP method to determine whether the user has followed the public account:

<?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 "未关注";
     
 }
Copy after login

The following is the second code case

< ? php
 
$access_token = $this - > _getAccessToken();
$subscribe_msg = &#39;https://api.weixin.qq.com/cgi-bin/user/info?access_token=&#39;.$access_token.&#39;&openid=&#39;.$_SESSION[&#39;wecha_id&#39;];
$subscribe = json_decode($this - > curlGet($subscribe_msg));
$zyxx = $subscribe - > subscribe;
 
if ($zyxx !== 1) {
 echo&#39;未关注!&#39;;
}
private function _getAccessToken() {
 $where = array(&#39;token&#39; = > $this - > token);
 $this - > thisWxUser = M(&#39;Wxuser&#39;) - > where($where) - > find();
 $url_get = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39;.$this - > thisWxUser[&#39;appid&#39;].&#39;&secret=&#39;.$this - > thisWxUser[&#39;appsecret&#39;];
 $json = json_decode($this - > curlGet($url_get));
 if (!$json - > errmsg) {
 } else {
 $this - > error(&#39;获取access_token发生错误:错误代码&#39;.$json - > errcode.&#39;,微信返回错误信息:&#39;.$json - > errmsg);
 }
 return $json - > access_token;
}
? >
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related articles:

About how to determine whether a user has entered a page for the first time?

Use PHP to determine if the user is logging in for the first time that day

How to use the PHP website to determine whether the user is accessing from a mobile phone

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!