php web qq第三方登录

官方api地址:http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
1.去qq互联上注册申请成为开发者,并创建一个应用获得appid,appkey
2.在需要QQ登录的页面加上qq小图标,并在图标外层a标签上加上 onclick='tologin()' 事件,底部加上script

 <script>
function toLogin(){
//以下为按钮点击事件的逻辑。注意这里要重新打开窗口
//否则后面跳转到QQ登录,授权页面时会直接缩小当前浏览器的窗口,而不是打开新窗口
var A=window.open("/ext/qq_login/index.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1");
}
</script>

3.下载sdk 官网下载地址:http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD
    a.下载了sdk后,将sdk放到项目中,例如当前放在了根目录,sdk命名为qq_login
    b.浏览器访问 yourdomain/qq_login/install/,进行安装配置
        qq互联上配置应用的callback为 yourdomain/qq_login/callback.php,这里安装的配置也应该填 yourdomain/qq_login/callback.php
    c.重命名或删除掉install
    d.修改sdk根目录index.php 内容为

 <?php
require_once("./API/qqConnectAPI.php");
$qc = new QC();
$qc->qq_login();

e.在sdk根目录即index.php同级目录建一个文件叫callback.php,callback.php即成功调用qq登录后执行的操作
4.登录成功,访问callback.php文件
    登录成功时会跳转到你配置的callback中,url上会带有一个get请求的code参数
    a.接收code参数,利用code参数和你的appid,appkey获取access_token
    b.获取到的access_token即为用户唯一标识符,你就可以用来注册到自己数据库中了

 <?php
session_start();
/**
* Step1:获取Authorization Code
* Step2:通过Authorization Code获取Access Token
* author: porter
*/
define("HOST_INFO",'http://'.$_SERVER['HTTP_HOST']);
$API_ID = "xx";
$API_KEY = "xx";
$code = $_GET['code'];
// 根据code获取access_token
// grant_type 必须 授权类型,在本步骤中,此值为“authorization_code”。
// client_id 必须 申请QQ登录成功后,分配给网站的appid。
// client_secret 必须 申请QQ登录成功后,分配给网站的appkey。
// code 必须 上一步返回的authorization code。
// 如果用户成功登录并授权,则会跳转到指定的回调地址,并在URL中带上Authorization Code。
// 例如,回调地址为www.qq.com/my.php,则跳转到:
// http://www.qq.com/my.php?code=520DD95263C1CFEA087******
// 注意此code会在10分钟内过期。
// redirect_uri 必须 与上面一步中传入的redirect_uri保持一致。
$url = "https://graph.qq.com/oauth2.0/token";
$url .= "?grant_type=authorization_code";
$url .= "&client_id=$API_ID";
$url .= "&client_secret=$API_KEY";
$url .= "&code=$code";
$url .= "&redirect_uri=http://test.www.xx.com/ext/qq_login/callback.php";
$resultStr = file_get_contents($url);
// 解析返回的数据
$resultArr = explode('&' , $resultStr);
$result = array();
foreach($resultArr as &$v){
list($key , $value) = explode('=' , $v);
$result[$key] = $value;
}
// 将返回结果中的access_token进行登录操作
$data['uuid'] = $result['access_token'];
$data['type'] = 'qq';
$data['s'] = 'asdf'; //用户模拟手机请求
$ch = curl_init (); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, HOST_INFO."/user/tlogin.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$return = curl_exec($ch);
curl_close($ch);
// 登录成功记录session
$return = json_decode($return , true);
if($return['status'] == 1){
$_SESSION['user'] = $return['data'];
}
header("Location:".HOST_INFO);
?>

可能出现的问题:
    QQ登录时提示"redirect *** 100010"这样的:
        你qq互联配置的callback与项目的callback不一样

在callback.php文件中登录成功保存了session,用header方法跳转页面,session无法传递: 
        在callback.php第一行写上  session_start();

上一篇:thinkphp5.0 QQ第三方登录详解


下一篇:[C#基础知识] ReadOnly关键字修饰的变量可以修改,只是不能重新分配