微信公眾號網頁授權獲取用戶基本信息
話不多說,根據官方文檔需求說明,首先接口權限設置修改
先根據你的URL配置一下授權回調頁域名
微信公眾平臺OAuth2.0授權的詳細步驟如下
1)用戶關註微信公眾號。
2)微信公眾號提供用戶請求授權頁面URL。
3)用戶點擊授權頁面URL,將向服務器發送請求。
4)服務器詢問用戶是否同意授權給微信公眾號(scope為snsapi_base時無此步驟)。
5)用戶同意(scope為snsapi_base時無此步驟)。
6)服務器將code通過回調傳給微信公眾號。
7)微信公眾號獲得code。
8)微信公眾號通過code向服務器請求access token.
9)服務器返回access token和Openid給微信公眾號.
10)微信公眾號通過access token向服務器請求用戶信息(scope為snsapi_base時無此步驟)
11)服務器將用戶信息回送給微信公眾號(scope為snsapi_base時無此步驟)
下面簡述一下snsapi_base與snsapi_userinfo區別
1、以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,並且是靜默授權並自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面)
2、以snsapi_userinfo為scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,並且由於用戶同意過,所以無須關註,就可在授權後獲取該用戶的基本信息。
3、用戶管理類接口中的“獲取用戶基本信息接口”,是在用戶和公眾號產生消息交互或關註後事件推送後,才能根據用戶OpenID來獲取用戶基本信息。這個接口,包括其他微信接口,都是需要該用戶(即openid)關註了公眾號後,才能調用成功的。
下面直接上授權請求代碼
這個就是拼接請求的接口代碼 weixin.class.php
<?php
class class_weixin
{
var $appid = "youappid";
var $appsecret = "youappsecret";
//構造函數,獲取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
}
//生成OAuth2的URL
public function oauth2_authorize($redirect_url, $scope, $state = NULL)
{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
return $url;
}
//生成OAuth2的Access Token
public function oauth2_access_token($code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
$res = $this->http_request($url);
return json_decode($res, true);
}
//獲取用戶基本信息(OAuth2 授權的 Access Token 獲取 未關註用戶,Access Token為臨時獲取)
public function oauth2_get_user_info($access_token, $openid)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
//獲取token
public function token(){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=".client_credential."&appid=".$this->appid."&secret=".$this->appsecret."";
$res = $this->http_request($url);
return json_decode($res, true);
}
//獲取用戶基本信息
public function get_user_info($access_token,$openid)
{
//$url = "https//api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$url= "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
//HTTP請求(支持HTTP/HTTPS,支持GET/POST)
protected function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
下面是授權獲取微信用戶個人信息的請求代碼
<?php
require_once(‘weixin.class.php‘);
$weixin = new class_weixin();
if (!isset($_GET["code"])){ //判斷有沒有獲取到code值,code相當於是和微信服務器請求的憑證,來換取access token
$redirect_url = ‘https://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘];
$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_userinfo", "123");
Header("Location: $jumpurl");
}else{
$access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
$token=$weixin->token();
$userinfo = $weixin->get_user_info($token["access_token"], $access_token_oauth2[‘openid‘]);
var_dump($userinfo); //這邊就打印出了用戶的個人信息了
?>
下面附上打印出來的結果
array(16) { ["subscribe"]=> int(1) ["openid"]=> string(28) "okGKvv6xpZ4FzfPBB5CbPkqdH-QI" ["nickname"]=> string(12) "匠人精神" ["sex"]=> int(1) ["language"]=> string(5) "zh_CN" ["city"]=> string(0) "" ["province"]=> string(0) "" ["country"]=> string(36) "南喬治亞島和南桑德韋奇島" ["headimgurl"]=> string(138) "http://thirdwx.qlogo.cn/mmopen/nbQibjkByWwve7MQlPQgceoKJQmZRJwmibMcHfnmIeltGuOdkdwCOlxicic7wBlhHnicmE9r6gORKq18wFRyPia07Via3YUn2OJ5ghe/132" ["subscribe_time"]=> int(1533544648) ["remark"]=> string(0) "" ["groupid"]=> int(0) ["tagid_list"]=> array(0) { } ["subscribe_scene"]=> string(17) "ADD_SCENE_QR_CODE" ["qr_scene"]=> int(0) ["qr_scene_str"]=> string(0) "" }
微信公眾號網頁授權獲取用戶基本信息