必要條件:
1)公衆號認證
2)有網頁授權獲取用戶基本信息的權限接口
填寫授權回調頁面的域名
登錄公衆平臺-->開發者中心-->接口權限表
找到 網頁授權獲取用戶基本信息 然後修改-->填寫你的域名.
---------------------------------------------------
關於網頁授權的兩種scope的區別說明(官方)
1、以snsapi_base爲scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,並且是靜默授權並自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面)
2、以snsapi_userinfo爲scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,並且由於用戶同意過,所以無須關注,就可在授權後獲取該用戶的基本信息。
3、用戶管理類接口中的“獲取用戶基本信息接口”,是在用戶和公衆號產生消息交互或關注後事件推送後,才能根據用戶OpenID來獲取用戶基本信息。這個接口,包括其他微信接口,都是需要該用戶(即openid)關注了公衆號後,才能調用成功的。
因爲scope有兩中模式,所以下面分開解說:
scope爲snsapi_base 那麼用戶必須是關注了公衆號才能取得信息
代碼實例
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); } |
測試步驟:
創建index.php和getUserInfo.php兩個文件後
先測試:scope爲snsapi_base
1)先關注公衆賬號
2)將網址: http://你的域名/index.php 生成一個二維碼! (草料二維碼)
3)用微信掃一掃
再測試:scope爲snsapi_userinfo
1)替換代碼
2)取消關注當前公衆號.
3)然後用微信掃一掃,剛剛你生成的二維碼.