How to determine whether the user follows the WeChat official account in PHP

墨辰丷
Release: 2023-03-29 09:40:02
Original
4216 people have browsed it

This article mainly introduces in detail PHP to determine whether the user pays attention to the WeChat public account. It has a certain reference value. Interested friends can refer to it

I will do a poll on the WeChat platform recently. For activities, you need to follow the official account before you can participate in voting. So, how to judge whether the user has followed the official account?

The first idea is to get the watch list of the public account 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:

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 = '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;
}
? >
Copy after login

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

Related recommendations:

PHP access to Alipay instant payment function

PHP two-dimensional array deduplication algorithm graphic and text Detailed explanation

phpDetailed explanation of the three methods of obtaining POST data

The above is the detailed content of How to determine whether the user follows the WeChat official account in PHP. For more information, please follow other related articles on the PHP Chinese website!

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 [email protected]
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!