微信公眾號獲取未關注的用戶基本信息信息(不適用測試號)


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獲取用戶基本信息*/  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM