微信公众号获取未关注的用户基本信息信息(不适用测试号)

1、根据公众号的appid获取code  

$APPID=APPID;//公众号在微信的appid  

$REDIRECT_URI='http://www.ific.cc/check.php';//回调页面      

// $scope='snsapi_base';  

$scope='snsapi_userinfo';//需要授权  

$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&redirect_uri=".urlencode($REDIRECT_URI)."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";  

header("Location:".$url);  

特:  

 -->此处必须修改公众号的获取用户信息的回调url  

2.check.php页面  

$code = $_GET['code'];  

$state = $_GET['state'];  

/*根据code获取用户openid*/  

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx72e1ef917e46fc68&secret=eb209bfaa8effa31f4508cea9788f5d0&code=".$code."&grant_type=authorization_code";  

$abs = file_get_contents($url);  

$obj=json_decode($abs);  

$access_token = $obj->access_token;  

$openid = $obj->openid;  

/*根据code获取用户openid end*/  

/*根据用户openid获取用户基本信息*/  

$abs_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";

 $abs_url_data = file_get_contents($abs_url);  

$obj_data=json_decode($abs_url_data);  

echo $OpenId = $obj_data->openid;  

echo $NickName = $obj_data->nickname;  

/*根据用户openid获取用户基本信息*/  

posted @ 2016-06-29 14:01  shakeus  阅读(8036)  评论(0编辑  收藏  举报