Files
youlegames/codes/agent/game/dlweb/api/document/微信开发判断用户是否已关注公众号.txt
2026-03-15 01:27:05 +08:00

46 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
在做微信开发时有时需要判断用户是否已关注公众号,以便于做出不同的回应,如提醒用户关注公众号等。
判断用户是否已关注公众号可能通过获取用户信息来操作。
如果已关注公众号那么微信返回的JSON数据包会包含”subscribe”: 1这样的信息。
如果未关注公众号那么微信返回的JSON数据包会包含”subscribe”: 0这样的信息。
开发者可通过OpenID到http GET请求方式调用接口来获取用户基本信息。
请求示例:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
参数:
参数 是否必须 说明
access_token 是 调用接口凭证
openid 是 普通用户的标识,对当前公众号唯一
lang 否 返回国家地区语言版本zh_CN 简体zh_TW 繁体en 英语
以下是PHP来判断用户是否已关注公众号的示例
function verification($openid,$accestoken){
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$accestoken.'&openid='.$openid.'&lang=zh_CN';
$json_Wxuser=json_decode(curlGet($url),true);
//获取用户信息后判断subcribe == 0 那么就是未关注
if(isset($json_Wxuser['subscribe']) && $json_Wxuser['subscribe'] == 0){
//跳转到提示用户关注页面中
$this->redirect('Guanzhu/index', array('id' => $id), 0, '页面跳转中...');
}
}
function curlGet($url){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}