<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/  class login_qq { private static $instance; public static function init(){ if (is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } function __construct($params, $referer_url, $state){ $this->app_id = $params['app_key']; $this->app_secret = $params['app_secret']; $this->redirect_uri = $referer_url; $this->state = $state; } function jump_url(){ $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='.$this->app_id.'&state='.$this->state.'&scope=get_user_info,do_like&redirect_uri='.urlencode($this->redirect_uri); header("Location:".$url); } function getAccessToken($code){ $url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id='.$this->app_id.'&code='.$code.'&client_secret='.$this->app_secret.'&redirect_uri='.urlencode($this->redirect_uri); $response = curl::get($url); if(strpos($response, "callback") !== false){ $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos -1); $msg = json_decode($response); if(isset($msg->error)){ showmsg('授权失败:'.$msg->error); } } $params = array(); parse_str($response, $params); $me_url= 'https://graph.qq.com/oauth2.0/me?access_token='.$params["access_token"]; $me_response = curl::get($me_url); if(strpos($me_response, "callback") !== false){ $lpos = strpos($me_response, "("); $rpos = strrpos($me_response, ")"); $me_response = substr($me_response, $lpos + 1, $rpos - $lpos -1); } $user = json_decode($me_response); if(isset($user->error)){ showmsg('获取信息失败：'.$user->error_description); } $data['openid'] = $user->openid; $data['access_token'] = $params['access_token']; return $data; } function get_info($access_token, $openid){ $url = 'https://graph.qq.com/user/get_user_info?access_token='.$access_token.'&oauth_consumer_key='.$this->app_id.'&openid='.$openid; $response = curl::get($url); $result = json_decode($response); $data['nick'] = $result->nickname; $data['avatar'] = $result->figureurl_qq_2; $data['sex'] = $result->gender; return $data; } }