最近实现了QQ登录,qq第三方登录步骤就是首先获取appid,appkey,填写callback;

拼接登录url,带上自己的应用信息,

通过callback地址获取token,通过token获取登陆者openid,通过openid获取用户信息。详细的可以去qq互联上看。

接下来说thinkphp3.2如何使用,首先我封装了自己的qqAuth类,代替了官方的SDK。很简洁实用。

这个类就长这个样子:

<?php
namespace Vendor\qq;

class qqAuth{

	//申请到的appid
	private $appid='';
	//申请到的appkey
	private $appkey='';
	//QQ登录成功后跳转的地址,请确保地址真实可用,否则会导致登录失败。
	private $callback='';
	//QQ授权api接口.按需调用
	private $scope='';

	public function __construct($appid,$appkey,$callback,$scope=''){
		$this->appid=$appid;
		$this->appkey=$appkey;
		$this->callback=$callback;
		if(empty($scope)){
			$this->scope='get_user_info,add_share,list_album,add_album,
				upload_pic,add_topic,add_one_blog,add_weibo';			
		}else{
			$this->scope=$scope;
		}
	}

	/**
	 * QQ登录
	 */
	function qqLogin(){
	    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF 保护
	    $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
	        . $this->appid . "&redirect_uri=" . urlencode($this->callback)
	        . "&state=" . $_SESSION['state']
	        . "&scope=".$this->scope;
	    header("Location:$login_url");
	}

	/**
	 * 回调函数
	 */
	function qqCallback(){
	    if($_REQUEST['state'] == $_SESSION['state']){
	        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
	            . "client_id=" . $this->appid. "&redirect_uri=" . urlencode($this->callback)
	            . "&client_secret=" . $this->appkey. "&code=" . $_REQUEST["code"];
	        $response = file_get_contents($token_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)){
	                echo "<h3>error:</h3>" . $msg->error;
	                echo "<h3>msg  :</h3>" . $msg->error_description;
	                exit;
	            }
	        }
	        $params = array();
	        parse_str($response, $params);
	        $_SESSION["access_token"] = $params["access_token"];
	    }else{
	        echo("The state does not match. You may be a victim of CSRF.");
	    }
	}

	/**
	 * 获取用户openid
	 */
	function getOpenid(){
	    $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" 
	        . $_SESSION['access_token'];
	    $str  = file_get_contents($graph_url);
	    if (strpos($str, "callback") !== false){
	        $lpos = strpos($str, "(");
	        $rpos = strrpos($str, ")");
	        $str  = substr($str, $lpos + 1, $rpos - $lpos -1);
	    }
	    $user = json_decode($str);
	    if (isset($user->error)){
	        echo "<h3>error:</h3>" . $user->error;
	        echo "<h3>msg  :</h3>" . $user->error_description;
	        exit;
	    }
	    $_SESSION["openid"] = $user->openid;
	}

	/**
	 * 获取用户信息
	 */
	function getUserInfo(){
	    $get_user_info = "https://graph.qq.com/user/get_user_info?"
	        . "access_token=" . $_SESSION['access_token']
	        . "&oauth_consumer_key=" . $this->appid
	        . "&openid=" . $_SESSION["openid"]
	        . "&format=json";
	    $info = file_get_contents($get_user_info);
	    $arr = json_decode($info, true);
	    return $arr;
	}

}
将这个类放到ThinkPHP/Library/Vendor/qq目录下,然后再需要用到的地方use
use \Vendor\qq\qqAuth;
接下来就是登陆方法了,首先在页面上放置在qq登陆按钮:
未登录?
<a onclick='toQzoneLogin()'><img src="__PUBLIC__/static/home/images/qqlogin.png"></a>
js代码如下:
function toQzoneLogin() {
    var _url='/index/qqlogin';  //转向网页的地址; 
    var name='TencentLogin';    //网页名称,可为空; 
    var iWidth=450; //弹出窗口的宽度; 
    var iHeight=320;   //弹出窗口的高度; 
    //获得窗口的垂直位置 
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2; 
    //获得窗口的水平位置 
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; 
    window.open(_url, name, 'height=' + iHeight +
        ',innerHeight=' + iHeight + ',width=' + iWidth +
        ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + 
        ',status=1,toolbar=no,menubar=no,location=1,resizable=no,scrollbars=0,titlebar=no');
}
然后写qqlogin方法:
/**
 * QQ登陆
 * @return [type] [description]
 */
public function qqLogin(){
    $qqlogin=new qqAuth(F('webSet')['appid'],F('webSet')['appkey'],F('webSet')['callback']);
    $qqlogin->qqLogin();
}

/**
 * QQ登陆回调函数
 * @return [type] [description]
 */
public function qqlogincallback(){
    $qqlogin=new qqAuth(F('webSet')['appid'],F('webSet')['appkey'],F('webSet')['callback']);
    $qqlogin->qqCallback();
    $qqlogin->getOpenid();
    //此处是为了跳转到saveVisitor方法,此方法记录登陆者信息,然后把小窗口关掉
    echo "<script>opener.location.href='/index/saveVisitor.html';window.close();</script>";
}

实例化qqAuth类需要传三个参数,就是你在qq互联上申请到的appid,appkey,填写的callback地址。

在saveVisitor函数里我获取了用户信息,用以下代码获取:

$qqlogin=new qqAuth(F('webSet')['appid'],F('webSet')['appkey'],F('webSet')['callback']);
$visitor=$qqlogin->getUserInfo();
至于获取到了什么信息,就是昵称,openid,birthyear,地址等信息,根据个人需要处理吧。
相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!