判断是否关注了微信公众号 subscribe 0=未关注 1=已关注

$appid='';
$secret='';
//微信网页授权获取openid
$web_url='http://www.xxxx.com/shouquan.php';
if (!isset($_GET['code'])) {
	$redirect_uri=urlencode($web_url);
	$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
	header("location:$url");exit();
}
$code=trim($_GET['code']);
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$access=file_get_contents($url);
$data=json_decode($access,true);
$access_token=$data['access_token'];
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN';
$user=file_get_contents($url);
$arr=json_decode($user,true);
//获取用户的openid
$openid=$arr['openid'];

$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$access=file_get_contents($url);
$access_arr=json_decode($access,true);
//非网页的access_token
$access_token=$access_arr['access_token'];

$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res=file_get_contents($url);
var_dump($res);

 

subscribe 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
posted @ 2018-07-16 16:29  zhuzhipeng  阅读(4886)  评论(0编辑  收藏  举报