网站QQ登录API接口的完整代码
展开阅读全文

太难受了!以前巴不得QQ登录,结果一直提示有问题,好不容易今天搞定了,却发现我的网站定位已经不适合QQ登录了!

不过好在还是解决了这个登录问题,算是解放了心中的念头。代码如下:

<?php
class QQConnect {
    //发送get请求
    private function get($url, $param = null) {
        if($param != null) {
            $query = http_build_query($param);
            $url = $url . '?' . $query;
        }
        $ch = curl_init();
        if(stripos($url, "https://") !== false){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $content = curl_exec($ch);
        $status = curl_getinfo($ch);
        curl_close($ch);
        if(intval($status["http_code"]) == 200) {
            return $content;
        }else{
            echo $status["http_code"];
            return false;
        }
    }
    private function get_access_token($appkey, $appsecretkey, $code, $callback, $state) {
        if($state == session('state')) {
            $url = "https://graph.qq.com/oauth2.0/token";
            $param = array(
                "grant_type"    =>    "authorization_code",
                "client_id"     =>    $appkey,
                "client_secret" =>    $appsecretkey,
                "code"          =>    $code,
                "state"         =>    $state,
                "redirect_uri"  =>    $callback
            );
            $response = $this->get($url, $param);
            if($response == false) {
                return false;
            }
            $params = array();
            parse_str($response, $params);
            return $params["access_token"];
        } else {
            exit("The state does not match. You may be a victim of CSRF.");
        }
    }

    private function get_openid($access_token) {
        $url = "https://graph.qq.com/oauth2.0/me"; 
        $param = array(
            "access_token"    => $access_token
        );

        $response  = $this->get($url, $param);
        if($response == false) {
            return false;
        }
        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
        }

        $user = json_decode($response);
        if (isset($user->error) || $user->openid == "") {
            return false;
        }
        return $user->openid;
    }

    public function get_user_info($token, $openid, $appkey, $format = "json") {
        $url = "https://graph.qq.com/user/get_user_info";
        $param = array(
            "access_token"      =>    $token,
            "oauth_consumer_key"=>    $appkey,
            "openid"            =>    $openid,
            "format"            =>    $format
        );

        $response = $this->get($url, $param);
        if($response == false) {
            return false;
        }

        $user = json_decode($response, true);
        return $user;
    }

    public function login($appkey, $callback, $scope='') {
        session('state',md5(uniqid(rand(), TRUE))); //CSRF protection
        $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
            . $appkey . "&redirect_uri=" . urlencode($callback)
            . "&state=" . session('state')
            . "&scope=".$scope;
        header('Location:'.$login_url);
    }

    public function callback($appkey, $appsecretkey, $callback) {
        $code = input('code');
        $state = session('state');

        $token = $this->get_access_token($appkey, $appsecretkey, $code, $callback, $state);
        $openid = $this->get_openid($token);
        if(!$token || !$openid) {
            exit('get token or openid error!');
        }

        return array('openid' => $openid, 'token' => $token);
    }

}

使用如下:

这是刚登录所需的

<?php 
 require_once 'QQConnect.php';
        $qq = new \QQConnect();
        $client_id = '123456789';
        $redirect_url = 'https://www.lmcjl.com/qq';//回调地址
        $scope = 'get_user_info';
        $qq->login($client_id, $redirect_url, $scope = 'get_user_info');
?>

回调路径代码:

require_once 'QQConnect.php';
$client_id = '123456789';
$key = 'b413elmcjl5200084e010c37bb118412';
$redirect_url = 'https://www.lmcjl.com/qq';//回调地址
$qq = new \QQConnect();
$info = $qq->callback($client_id, $key, $redirect_url);
if(!isset($info['openid'])){
echo '获取唯一信息失败,<a href="/">点击返回首页</a>';exit;
}
$abc = $qq->get_user_info($info['token'], $info['openid'], $client_id);