php 微信授权登录
1 <?php 2 /** 3 *微信授权实例 4 */ 5 class ClassName extends AnotherClass 6 { 7 /** 8 * wechat oauth 之后的回调页面 9 */ 10 public function callback() 11 { 12 $this->_globals(); 13 if (!isset($_GET['code']) || !$_GET['code']) { 14 echo "无效的地址"; 15 exit; 16 } 17 if ($_SERVER['HTTP_HOST'] == _WECHAT_OAUTH_DOMAIN) { 18 //redirect to customer domain 19 $customDomain = $_GET['state']; 20 $url = "http://" . $customDomain . $_SERVER["REQUEST_URI"]; 21 header("Location: {$url}"); 22 exit; 23 } 24 $wechat = new Wechat($this->client['appid'], $this->client['appsecret']); 25 $accessToken = $wechat->getAccessToken($_GET['code']); //获取accessToken 26 $userInfo = $wechat->getUserInfo($accessToken['access_token'], $accessToken['openid']); //获取用户信息 27 28 $thirdUserMdl = model('CoreUserOpenids'); 29 $filter = array("client_id='{$this->client['id']}'", "utype='wechat'", "openid='{$userInfo['openid']}'"); 30 $res = $thirdUserMdl->getRow($filter); 31 32 $userMdl = model('CoreUsers'); 33 $user = $userMdl->getRow(array("id='{$res['uid']}'")); 34 35 $_SESSION['login_user'] = $user; 36 $_SESSION['wechat_user'] = $res; 37 38 $targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url']; 39 header('location:' . $targetUrl); 40 exit; 41 } 42 43 44 public function getAuthorizeUrl($redirectUri, $scope = 'snsapi_userinfo', $state = '') { 45 $baseurl = 'https://open.weixin.qq.com/connect/oauth2/authorize'; 46 $params = [ 47 'appid' => $this->appid, 48 'redirect_uri' => $redirectUri, 49 'response_type' => 'code', 50 'scope' => $scope, 51 'state' => $state, 52 ]; 53 $url = $baseurl . '?' . http_build_query($params); 54 return $url; 55 } 56 57 58 /** 59 * Notes:code 换access_token 60 * Created by xxg@seabig.cn 61 * Date: 2020/5/8 62 * Time: 0:38 63 */ 64 public function getAccessToken($code) { 65 $baseurl = 'https://api.weixin.qq.com/sns/oauth2/access_token'; 66 $params = [ 67 'appid' => $this->appid, 68 'secret' => $this->appsecret, 69 'code' => $code, 70 'grant_type' => 'authorization_code', 71 ]; 72 $url = $baseurl . '?' . http_build_query($params); 73 $ch = curl_init(); 74 curl_setopt($ch, CURLOPT_URL, $url); 75 curl_setopt($ch, CURLOPT_HEADER, 0); 76 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 77 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 78 curl_setopt($ch, CURLOPT_TIMEOUT, 15); 79 $res = curl_exec($ch); 80 curl_close($ch); 81 $json_obj = json_decode($res, true); 82 $access_token = $json_obj['access_token']; 83 $openid = $json_obj['openid']; 84 $data = array("access_token" =>$access_token , "openid" => $openid); 85 return $data; 86 } 87 88 /** 89 * Notes:获取用户信息 90 * Created by xxg@seabig.cn 91 * Date: 2020/5/8 92 * Time: 0:39 93 */ 94 public function getUserInfo($access_token,$openid) { 95 $baseurl = 'https://api.weixin.qq.com/sns/userinfo'; 96 $params = [ 97 'access_token' => $access_token, 98 'openid' => $openid, 99 'lang' => 'zh_CN', 100 ]; 101 $url = $baseurl . '?' . http_build_query($params); 102 $ch = curl_init(); 103 curl_setopt($ch, CURLOPT_URL, $url); 104 curl_setopt($ch, CURLOPT_HEADER, 0); 105 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 106 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 107 curl_setopt($ch, CURLOPT_TIMEOUT, 15); 108 $res = curl_exec($ch); 109 curl_close($ch); 110 //解析json 111 $userinfo = json_decode($res, true); 112 return $userinfo; 113 } 117 }