溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

發(fā)布時(shí)間:2021-06-04 13:48:12 來(lái)源:億速云 閱讀:135 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在這之前先給大家一個(gè)我自定義的請(qǐng)求接口的函數(shù),在下面的示例代碼中請(qǐng)求接口用的都是這個(gè)函數(shù)

該函數(shù)的作用是,想接口發(fā)起請(qǐng)求,傳遞參數(shù)并返回接口返回的數(shù)據(jù)

(這個(gè)里面的代碼就不做多解釋了,如果大家想要了解可以去看一下php curl函數(shù)總結(jié))

//自定義請(qǐng)求接口函數(shù),$data為空時(shí)發(fā)起get請(qǐng)求,$data有值時(shí)發(fā)情post請(qǐng)求
function http_url($url,$data=null){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    if(!empty($data)){
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    $res = curl_exec($ch);
    if(curl_errno($ch)){
        echo "error:".curl_error($ch);
        exit;
    }
    curl_close($ch);
    return $res;
}

(文中所使用的接口為騰訊官方提供,大家可以參考一下微信公眾平臺(tái)的開(kāi)發(fā)者文檔

一、首先我們需要配置我們的公眾號(hào)

1、在微信公眾號(hào)請(qǐng)求用戶(hù)網(wǎng)頁(yè)授權(quán)之前,開(kāi)發(fā)者需要先到公眾平臺(tái)官網(wǎng)中的“開(kāi)發(fā) - 接口權(quán)限 - 網(wǎng)頁(yè)服務(wù) - 網(wǎng)頁(yè)帳號(hào) - 網(wǎng)頁(yè)授權(quán)獲取用戶(hù)基本信息”的配置選項(xiàng)中,修改授權(quán)回調(diào)域名。請(qǐng)注意,這里填寫(xiě)的是域名(是一個(gè)字符串),而不是URL,因此請(qǐng)勿加 http:// 等協(xié)議頭;

2、授權(quán)回調(diào)域名配置規(guī)范為全域名,比如需要網(wǎng)頁(yè)授權(quán)的域名為:www.qq.com,配置以后此域名下面的頁(yè)面http://www.qq.com/music.html 、 http://www.qq.com/login.html 都可以進(jìn)行OAuth3.0鑒權(quán)。但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com無(wú)法進(jìn)行OAuth3.0鑒權(quán)

3、如果公眾號(hào)登錄授權(quán)給了第三方開(kāi)發(fā)者來(lái)進(jìn)行管理,則不必做任何設(shè)置,由第三方代替公眾號(hào)實(shí)現(xiàn)網(wǎng)頁(yè)授權(quán)即可

二、用戶(hù)同意授權(quán),獲取code

接口地址:https://open.weixin.qq.com/connect/oauth3/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect (注意接口參數(shù))

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

function Get_Code()  //獲取code
{
//構(gòu)造請(qǐng)求地址
$code_url = "https://open.weixin.qq.com/connect/oauth3/authorize?appid=微信公眾號(hào)appid&redirect_uri=請(qǐng)求功后回調(diào)地址&response_type=code&scope=snsapi_userinfo&state=STATE #wechat_redirect";
//跳轉(zhuǎn)到請(qǐng)求地址,應(yīng)為本省設(shè)置了回調(diào)地址,所以不需要使用file_get_content()來(lái)請(qǐng)求接口。
header("location:" . $code_url);
exit;
}

三、通個(gè)獲取到的code來(lái)或缺access_token和openid

接口:https://api.weixin.qq.com/sns/oauth3/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

/**
 *  通過(guò)獲取到的code來(lái)獲取access_token和openid 
 *  $code為獲取到的code
 * 接口的參數(shù)注意換成自己的,如appid和secret
 */
function GetAccess_Token($code)
{
$get_access_token_url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=appid&secret=secret&code=$code&grant_type=authorization_code";
$res = http_url($get_access_token_url);
return json_decode($res, true);
}

四、判斷access_token是否有效

接口:https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

/**
 * 檢查access_token是否有效
 * 
 */
function CkeckAccessToken($access_token, $openid)
{
    $check_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid";
    $res = http_url($check_url);
    $result = json_decode($res, true);
    if (isset($result['errmsg']) && $result['errmsg'] == 1) {
        return 1;       //access_token有效   
    } else {
        return 0;       //access_token無(wú)效 
    }
}

五、如果失效,刷新access_token

接口:https://api.weixin.qq.com/sns/oauth3/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

/**
 * 如果獲取到的access_token無(wú)效,通過(guò)refresh_token來(lái)刷新access_token
 *接口的參數(shù)注意換成自己的
 */
function GetRefresh_Token($refresh_token)
{
$get_refresh_token_url = "https://api.weixin.qq.com/sns/oauth3/refresh_token?appid=appid&grant_type=refresh_token&refresh_token=$refresh_token";
$res = http_url($get_refresh_token_url);
return json_decode($res, true);
}

六、獲取用戶(hù)信息

接口:https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析

/** * 獲取用戶(hù)基本信息 *  */
function Get_User_Info($access_token, $openid){   
     $get_user_info = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";   
      $res = http_url($get_user_info);   
       return json_decode($res, true);
   }

獲取到用戶(hù)信息數(shù)據(jù):

{   
    "openid":" OPENID",
    " nickname": NICKNAME,
    "sex":"1",
    "province":"PROVINCE"
    "city":"CITY",
    "country":"COUNTRY",
    "headimgurl":       "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
    "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
    "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}

微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析下面上完整代碼:

<?php
    //跳轉(zhuǎn)第三方頁(yè)面,獲取用戶(hù)基本信息
    // 這是請(qǐng)求頁(yè)面也是code的回調(diào)頁(yè)面
    session_start();                //啟動(dòng)session
    if (isset($_GET['code'])) {     //判斷是否有code傳過(guò)來(lái),如果沒(méi)有調(diào)用函數(shù)請(qǐng)求code
          $res = GetAccess_Token($_GET['code']);     //使用code獲取access_token和openid
          if (CkeckAccessToken($res['access_token'], $res['openid']) == 0) {     //判斷access_token是否有效,如果無(wú)效獲取新的access_token
                  $res = GetRefresh_Token($res['refresh_token']);                    //或缺新的access_token
            }
           $userinfo = Get_User_Info($res['access_token'], $res['openid']);        //獲取用戶(hù)信息
           $_SESSION['userinfo'] = $userinfo;                                      //將用戶(hù)信息存入session中
           $next_url = 'http://web/index.php';                                     //下一個(gè)頁(yè)面地址
           header("location:" . $next_url);                                       //獲取到信息后跳轉(zhuǎn)到其他頁(yè)面
           exit;
      } else { 
         //獲取code
      Get_Code();
      }
    function Get_Code()  //獲取code{
    $code_url = "https://open.weixin.qq.com/connect/oauth3/authorize?appid=appid&redirect_uri=回調(diào)地址&response_type=code&scope=snsapi_userinfo&state=STATE #wechat_redirect";
    header("location:" . $code_url);
    exit;
    }
    /**
    *  通過(guò)獲取到的code來(lái)獲取access_token和openid
    *
    */
    function GetAccess_Token($code){
        $get_access_token_url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=appid&secret=secret&code=$code&grant_type=authorization_code";
        $res = http_url($get_access_token_url);
        return json_decode($res, true);
        }
    /**
     * 檢查access_token是否有效
     *
    */
    function CkeckAccessToken($access_token, $openid){
    $check_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid"; 
    $res = http_url($check_url);
    $result = json_decode($res, true);
    if (isset($result['errmsg']) && $result['errmsg'] == 1) {
       return 1;       //access_token有效 
     } else { 
       return 0;       //access_token無(wú)效 
     }
    }

    /**
     * 如果獲取到的access_token無(wú)效,通過(guò)refresh_token來(lái)刷新access_token 
     */
    function GetRefresh_Token($refresh_token){
        $get_refresh_token_url = "https://api.weixin.qq.com/sns/oauth3/refresh_token?appid=appid&grant_type=refresh_token&refresh_token=$refresh_token";
        $res = http_url($get_refresh_token_url);
        return json_decode($res, true);
     }
    /**
     * 獲取用戶(hù)基本信息
     *
     */
    function Get_User_Info($access_token, $openid){
        $get_user_info = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $res = http_url($get_user_info);
        return json_decode($res, true);}
    //自定義請(qǐng)求接口函數(shù),$data為空時(shí)發(fā)起get請(qǐng)求,$data有值時(shí)發(fā)起post請(qǐng)求
    function http_url($url,$data=null){
       $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        if(!empty($data)){    
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
         }
         $res = curl_exec($ch);
         if(curl_errno($ch)){
           echo "error:".curl_error($ch);
           exit;
          }
          curl_close($ch);
          return $res;
          }

關(guān)于“微信公眾號(hào)網(wǎng)頁(yè)授權(quán)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI