1. 程式人生 > >微信開發第四篇:網頁授權驗證

微信開發第四篇:網頁授權驗證

public function app_web_detail(){
//1.獲取code
$scope = 'snsapi_userinfo';//授權作用域(snsapi_base或snsapi_userinfo)
//開發者的appid
$appid = 'xxx';
//自動跳轉到下面的方法
$redirect_uri = urlencode('http://www.xxx.com/Wechat/get_userinfo');//回撥地址,進行url編碼(code引數會發放到此頁面)
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$scope.'&state=xxx#wechat_redirect';

}
public function get_userinfo(){
//2.獲取網頁授權access_token及open_id
$code = $_GET['code'];//獲取到的code,用來換去access_token
//開發者的appid和appsecret

$appid = 'xxx';
$appsecret = 'xxxx';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$res = $this ->curl($url);//獲得網頁access_token,使用者open_id等資訊
$access_token = $res['access_token'];//獲得網頁access_token
$open_id = $res['open_id']; //獲得
使用者open_id
//3.拉取使用者詳細資訊
//使用者詳細資訊請求地址
$url_info = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN ';
$res_info = $this -> curl($url_info);//獲得使用者詳細資訊,包含暱稱,頭像等資訊
//自行列印檢視結果
}