PHP 微信网页授权(OAuth2.0) 的简单示例

前端之家收集整理的这篇文章主要介绍了PHP 微信网页授权(OAuth2.0) 的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣PHP 微信网页授权(OAuth2.0) 的简单示例的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。<br>

提要: 

  1. 建议对OAuth2.0协议做一个学习。 

  2. 微信官方文档和微信官网工具要得到充分利用。 

比较简单,直接帖源代码了。其中“xxxxxxxxxx”部分,是需要依据自己环境做替换的

/**
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
  * OAuth2.0微信授权登录实现
  *
  * @author zzy
  * @文件名:GetWxUserInfo.PHP
*/
// 回调地址
 $url = urlencode("http://www.xxxxxxxxx.com/GetWxUserInfo.PHP");
 // 公众号的id和secret
 $appid = 'xxxxxxxxx';
 $appsecret = 'xxxxxxxxx';
 session_start();
 
  
 // 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
 if(!isset($_GET['code']) && !isset($_SESSION['code'])){
  echo
  '<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6c11a252ff1d00c4
  &redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">
  <font style="font-size:30">授权</font></a>';
   
  exit;
 }
  
 // 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
 if (isset($_GET['code']) && !isset($_SESSION['token'])){
  $_SESSION['code'] = $_GET['code'];
   
  $url="https://api.weixin.qq.com/sns/oauth2/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);
  
 // 依据申请到的access_token和openid,申请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函数简单封装
 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;
 } 

 
 得到正确结果如下:
 
 Array
(
 [code] => 041GZI4l0tvGHg10N75l05FQ4l0GZI42
 [token] => Array
  (
   [access_token] => TWo6w5QMpzTZibu3FPh2k4EdC5bllp4sGeQkC4NbZtj-zti-ctZj1SrrNL1qGCf2lB1-6o3N7kh2bcxl5bxtQqJEGk1cq12l8CzF40R9XvA
   [expires_in] => 7200
   [refresh_token] => Iz3olCrkqPBOJvSSH2bOKvA09Sjvsp1c8Ltm7MvxxPfQXSbvI_WoVmzhjqASzwlMa7TAGgsg3mIJmaHjL7jrJHDqUF1jKbhd6GNDnLtXq0U
   [openid] => ota_XwQ4r_5nioVmshQ
   [scope] => snsapi_userinfo
  )
 
 [userinfo] => Array
  (
   [openid] => ota_XwQ4r_5nioVmshQq
   [nickname] => xx
   [sex] => 1
   [language] => zh_CN
   [city] => xx
   [province] => xx
   [country] => xx
   [headimgurl] => http://wx.qlogo.cn/mmopen/PiajxSqBRaELwee7rhrt2ibnkC1MEnu04WiaWrw9FkuPBbGOgnrMbynNoEuxicgXOetW5VqQbTrS4fZDXNvAWsz6GQ/0
   [privilege] => Array
    (
    )
 
  )
 
)
原文链接:https://www.f2er.com/php/527847.html

猜你在找的PHP相关文章