一、根據公衆號的appid獲取code php
$APPID=APPID;//公衆號在微信的appid json
$REDIRECT_URI='http://www.ific.cc/check.php';//回調頁面 api
// $scope='snsapi_base'; 微信
$scope='snsapi_userinfo';//須要受權 app
$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"; url
header("Location:".$url); spa
特: code
-->此處必須修改公衆號的獲取用戶信息的回調url token
2.check.php頁面 get
$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獲取用戶基本信息*/