微信開發網頁授權的兩種方式
阿新 • • 發佈:2018-12-27
第一步在微信公眾平臺中找到網頁授權
然後再填寫授權回撥頁面,這裡的頁面如同手冊裡面一樣 以xxx.xxxxx.com 即可 而且此處的域名一定要和程式碼中回撥地址在同一個域名之下。
關於網頁授權的兩種scope的區別說明(官方)
1、以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的使用者的openid的,並且是靜默授權並自動跳轉到回撥頁的。使用者感知的就是直接進入了回撥頁(往往是業務頁面)
2、以snsapi_userinfo為scope發起的網頁授權,是用來獲取使用者的基本資訊的。但這種授權需要使用者手動同意,並且由於使用者同意過,所以無須關注,就可在授權後獲取該使用者的基本資訊。
3、使用者管理類介面中的“獲取使用者基本資訊介面”,是在使用者和公眾號產生訊息互動或關注後事件推送後,才能根據使用者OpenID來獲取使用者基本資訊。這個介面,包括其他微信介面,都是需要該使用者(即openid)關注了公眾號後,才能呼叫成功的。
因為scope有兩中模式,所以下面分開解說:
scope為snsapi_base 那麼使用者必須是關注了公眾號才能取得資訊
先自己建立兩個檔案: index.php 和 getUserInfo.php
程式碼例項
index.php如下:
//scope=snsapi_base 例項 $appid='你的AppId'; $redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' ); $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; header("Location:".$url);
getUserInfo.php如下:
$appid = "你的AppId"; $secret = "你的AppSecret"; $code = $_GET["code"]; //第一步:取全域性access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; $token = getJson($url); //第二步:取得openid $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauth2 = getJson($oauth2Url); //第三步:根據全域性access_token和openid查詢使用者資訊 $access_token = $token["access_token"]; $openid = $oauth2['openid']; $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN"; $userinfo = getJson($get_user_info_url); //列印使用者資訊 print_r($userinfo); function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); }
scope為snsapi_userinfo 使用者不用關注公眾號,也能取到資訊,但是會有一個介面讓使用者去點選確認!相當於一個登入授權吧!
程式碼例項
index.php如下:
//scope=snsapi_userinfo例項
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
getUserInfo.php如下:
$appid = "你的AppId";
$secret = "你的AppSecret";
$code = $_GET["code"];
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第二步:根據全域性access_token和openid查詢使用者資訊
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info ?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//列印使用者資訊
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
這樣就可以直接在自己微信中進行測試,點選選單進行測試 跳轉頁面時即可