溫馨提示×

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

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

PHP源碼如何實(shí)現(xiàn)微信網(wǎng)頁授權(quán)OAuth2.0

發(fā)布時(shí)間:2021-08-27 09:30:07 來源:億速云 閱讀:202 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了PHP源碼如何實(shí)現(xiàn)微信網(wǎng)頁授權(quán)OAuth2.0,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

提要: 
  1. 建議對(duì)OAuth3.0協(xié)議做一個(gè)學(xué)習(xí)。 
  2. 微信官方文檔和微信官網(wǎng)工具要得到充分利用。 
比較簡單,直接帖源代碼了。其中“xxxxxxxxxx”部分,是需要依據(jù)自己環(huán)境做替換的

/**
  * OAuth3.0微信授權(quán)登錄實(shí)現(xiàn)
  *
  * @文件名:GetWxUserInfo.php
  */

 // 回調(diào)地址
 $url = urlencode("http://www.xxxxxxxxx.com/GetWxUserInfo.php");
 // 公眾號(hào)的id和secret
 $appid = 'xxxxxxxxx';
 $appsecret = 'xxxxxxxxx';
 session_start();

 
 // 獲取code碼,用于和微信服務(wù)器申請(qǐng)token。 注:依據(jù)OAuth3.0要求,此處授權(quán)登錄需要用戶端操作
 if(!isset($_GET['code']) && !isset($_SESSION['code'])){
  echo 
  '<a href="https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx6c11a252ff1d00c4
  &redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">
  <font >授權(quán)</font></a>';
  
  exit;
 }
 
 // 依據(jù)code碼去獲取openid和access_token,自己的后臺(tái)服務(wù)器直接向微信服務(wù)器申請(qǐng)即可
 if (isset($_GET['code']) && !isset($_SESSION['token'])){
  $_SESSION['code'] = $_GET['code'];
  
  $url="https://api.weixin.qq.com/sns/oauth3/access_token?appid=".$appid.
   "&secret=".$appsecret."&code=".$_GET['code']."&grant_type=authorization_code";
  $res = https_request($url);
  $res=(json_decode($res, true));
  $_SESSION['token'] = $res;
 }
 
 print_r($_SESSION);
 
 // 依據(jù)申請(qǐng)到的access_token和openid,申請(qǐng)Userinfo信息。
 if (isset($_SESSION['token']['access_token'])){
  $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$_SESSION['token']['access_token']."&openid=".$_SESSION['token']['openid']."&lang=zh_CN";
  echo $url;
  $res = https_request($url);
  $res = json_decode($res, true);
  
  $_SESSION['userinfo'] = $res;

 }
 
 print_r($_SESSION);

 // cURL函數(shù)簡單封裝
 function https_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, 1);
  $output = curl_exec($curl);
  curl_close($curl);
  return $output;
 }

得到正確結(jié)果如下:

Array
(
 [code] => 041GZI4l0tvGHg10N75l05FQ4l0GZI42
 [token] => Array
  (
   [access_token] => TWo6w5QMpzTZibu3FPh3k4EdC5bllp4sGeQkC4NbZtj-zti-ctZj1SrrNL1qGCf2lB1-6o3N7kh3bcxl5bxtQqJEGk1cq12l8CzF40R9XvA
   [expires_in] => 7200
   [refresh_token] => Iz3olCrkqPBOJvSSH2bOKvA09Sjvsp1c8Ltm7MvxxPfQXSbvI_WoVmzhjqASzwlMa7TAGgsg3mIJmaHjL7jrJHDqUF1jKbhd6GNDnLtXq0U
   [openid] => ota_XwQ4r_5nioVmshQ
   [scope] => snsapi_userinfo
  )

 [userinfo] => Array
  (
   [openid] => ota_XwQ4r_5nioVmshQq
   [nickname] => 野狐
   [sex] => 1
   [language] => zh_CN
   [city] => 杭州
   [province] => 浙江
   [country] => 中國
   [headimgurl] => http://wx.qlogo.cn/mmopen/PiajxSqBRaELwee7rhrt2ibnkC1MEnu04WiaWrw9FkuPBbGOgnrMbynNoEuxicgXOetW5VqQbTrS4fZDXNvAWsz6GQ/0
   [privilege] => Array
    (
    )

  )

)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“PHP源碼如何實(shí)現(xiàn)微信網(wǎng)頁授權(quán)OAuth2.0”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

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

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

AI