PHP調用微信登陸授權


一、配置公眾號的appid、appsecret等信息

 

二、配置成功后返回的url

 

三、在返回的頁面接收參數,執行相關操作

 

<?php
/**
 * 獲取微信用戶信息
 */

class GetWxUser{    
    private $appid = 'appidddd';
    private $appsecret = 'appsecrettttt';
   /**
    * 1、獲取微信用戶信息,判斷有沒有code,有使用code換取access_token,沒有去獲取code。
    * @return array 微信用戶信息數組
    */
    public function get_user(){
        if (!isset($_GET['code'])){//沒有code,去微信接口獲取code碼
            $callback =  $_SERVER["REQUEST_SCHEME"]."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?url=".$_GET["url"];//微信服務器回調url,這里是本頁url
            $this->get_code($callback);
        } else {//獲取code后跳轉回來到這里了
            $code = $_GET['code'];
            $data = $this->get_access_token($code);//獲取網頁授權access_token和用戶openid
            $data_all = $this->get_user_info($data['access_token'],$data['openid'],$_GET["url"]);//獲取微信用戶信息
            return $data_all;
        }
    }
   /**
    * 2、用戶授權並獲取code
    * @param string $callback 微信服務器回調鏈接url
    */
    private function get_code($callback){
        $appid = $this->appid;
        $scope = 'snsapi_userinfo';
        $state = md5(uniqid(rand(), TRUE));//唯一ID標識符絕對不會重復
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri='.urlencode($callback).'&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
        header("Location:$url");
    }    
   /**
    * 3、使用code換取access_token
    * @param string 用於換取access_token的code,微信提供
    * @return array access_token和用戶openid數組
    */
    private function get_access_token($code){
        $appid = $this->appid;
        $appsecret = $this->appsecret;    
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
               echo 'error:' . $user->errcode.'<hr>msg  :' . $user->errmsg;
              exit;
        }
        $data = json_decode(json_encode($user),true);//返回的json數組轉換成array數組
        return $data;
    }    
   /**
    * 4、使用access_token獲取用戶信息
    * @param string access_token
    * @param string 用戶的openid
    * @return array 用戶信息數組
    */
    private function get_user_info($access_token,$openid,$pth){
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
        $user = file_get_contents($url);
        if (isset($user->errcode)) {
               echo 'error:' . $user->errcode.'<hr>msg  :' . $user->errmsg;
              exit;
        }
          $s=$pth==''?"?":"&";
    // var_dump( $_SERVER);
      echo $_SERVER["REQUEST_SCHEME"]."://".$_SERVER['HTTP_HOST']."/".$pth.$s."val=".$user;
     header("Location:". $_SERVER["REQUEST_SCHEME"]."://".$_SERVER['HTTP_HOST']."/quest/Assessment/index.php 這里配置返回地址".$pth.$s."val=".urlencode($user));
        return $user;
    }    
}
$g=new GetWxUser();
$g->get_user();
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM