针对第三方的话,我个人的建议是在本地新建虚拟主机,修改HOST文件后,把虚拟主机的对应的域名在本地重定向后可以在本地测试。
首先是需要下载SDK文件了。这里的话我打包给大家,下载地址:点我
下载完成之后,把这个文件解压后 放入 Think/Library/Org
我们需要做的是,第一步,在QQ互联上申请。
申请地址:点我
首页进去-》点击网站接入-》创建应用-》选择网站然后填写相关信息,然后网站头部需要加入meta的标签,因为这个问题,所以本地虚拟主机的重定向域名是需要和主机的网站一样的。
回调地址的话,现在可以直接和网站一样,后期会修改这里的。
填写完成后会进入这个应用的信息编辑拿到几个值:
APPID APPKEY 这两个值我们会在后面用到。
填写之后,我们需要在首页新建链接登陆,
<eq name="Think.session.uid" value=""> <a href="{:U('User/Member/loginqq',array("type"=>"qq"))}">QQ登陆</a> <else /> <a>{$Think.session.user}</a> </eq>
当我们点击这个控制器的时候,我们需要些对应的方法
// QQ登陆 public function loginqq($type = null) { empty($type) && $this->error('参数错误'); import('Org.ThinkSDK.ThinkOauth'); $sns = ThinkOauth::getInstance($type); redirect($sns->getRequestCodeURL()); }
然后这里因为引入SDK了,所以这个控制器的头部需要use下
<?php namespace User\Controller; use Think\Controller; use ThinkOauth;//因为在第三方类中没有添加namespace,所以是import引入的
这个时候会触发回调函数的方法,需要我们添加配置。在 Application/Common/Conf/下新建 sdk.php
<?php $SITE_URL = "http://www.webname.com/"; define('URL_CALLBACK', "" . $SITE_URL . "User/Member/callback?type="); return array( //腾讯QQ登录配置 'THINK_SDK_QQ' => array( 'APP_KEY' => '101258388', //应用注册成功后分配的 APP ID 'APP_SECRET' => '3b94b27f08833413d6323e1558666e55', //应用注册成功后分配的KEY 'CALLBACK' => URL_CALLBACK . 'qq', ), //新浪微博配置 'THINK_SDK_SINA' => array( 'APP_KEY' => '', //应用注册成功后分配的 APP ID 'APP_SECRET' => '', //应用注册成功后分配的KEY 'CALLBACK' => URL_CALLBACK . 'sina', ), //人人网配置 'THINK_SDK_RENREN' => array( 'APP_KEY' => '', //应用注册成功后分配的 APP ID 'APP_SECRET' => '', //应用注册成功后分配的KEY 'CALLBACK' => URL_CALLBACK . 'renren', ) );
写上我们之前的APPID APPKEY,这里我们需要去修改回调函数哪里了,回调的话改成 http://www.webname.com/User/Member/callback 然后才不会报错
配置话,还需要在主配置中加载
//加载数据库配置 'LOAD_EXT_CONFIG'=>'db,mail,sdk',
这时候走到callback回来的时候,我们需要去获取用户数据了
// QQ登录 public function callback($type = null, $code = null) { header("Content-type: text/html; charset=utf-8"); (empty($type) || empty($code)) && $this->error('参数错误'); import('Org.ThinkSDK.ThinkOauth'); $sns = ThinkOauth::getInstance($type); $extend = null; if ($type == 'tencent') { $extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey')); } $tokenArr = $sns->getAccessToken($code, $extend); $openid = $tokenArr['openid']; $token = $tokenArr['access_token']; session("openid", $openid); session("access_token", $token); if ($openid) { $field = strtolower($type); session("field", $field); $userinfo = M("user")->field('u_id,u_user')->where("" . $field . "= '" . $openid . "'")->find(); if ($userinfo) { //若是有该账号就登录 session("uid", $userinfo['u_id']); session("user", $userinfo['u_user']); echo "<script>document.location.href='" . __APP__ . "';</script>"; exit; } else { //没有的话绑定 $userid = session('uid'); $username = session('user'); if ($userid != '' && $username != '') { //用户已登录,自动绑定 //绑定账号 M('user')->where("u_id = " . $userid . "")->save(array($field => $openid)); session('type'); session('openid'); $this->success("绑定成功!", "/"); } else { //用户未登录,跳转到绑定页面 if ($filed == 'qq') { //针对新版qq互联在绑定页,要显示昵称,否则不通过*** $data = $sns->call('user/get_user_info'); $nickname = $data['nickname']; } else { $userinfo = A('Type', 'Event')->$type($tokenArr);//**** $nickname = $userinfo['name']; } session('nickname', $nickname); $this->redirect("/User/Member/bind"); } } } else { echo "<script>alert('系统出错;请稍后再试!');document.location.href='" . __APP__ . "';</script>"; } }
这里部分逻辑是我自己项目中用到的,因为基本都比较简单,我就不单独扣出来了。
在****的注释的地方是一个事件,这里也把代码给大家。
在User模块下的Event下TypeEvent.class.php
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: long <admin@loveteemo.com> <http://www.loveteemo.com> // +---------------------------------------------------------------------- // TypeEvent.class.php 2015-07-07 namespace User\Event; use ThinkOauth; class TypeEvent{ //登录成功,获取腾讯QQ用户信息 public function qq($token){ $qq = ThinkOauth::getInstance('qq', $token); $data = $qq->call('user/get_user_info'); if($data['ret'] == 0){ $userInfo['type'] = 'QQ'; $userInfo['name'] = $data['nickname']; $userInfo['nick'] = $data['nickname']; $userInfo['head'] = $data['figureurl_2']; return $userInfo; } else { throw_exception("获取腾讯QQ用户信息失败:{$data['msg']}"); } } //登录成功,获取腾讯微博用户信息 public function tencent($token){ $tencent = ThinkOauth::getInstance('tencent', $token); $data = $tencent->call('user/info'); if($data['ret'] == 0){ $userInfo['type'] = 'TENCENT'; $userInfo['name'] = $data['data']['name']; $userInfo['nick'] = $data['data']['nick']; $userInfo['head'] = $data['data']['head']; return $userInfo; } else { throw_exception("获取腾讯微博用户信息失败:{$data['msg']}"); } } //登录成功,获取新浪微博用户信息 public function sina($token){ $sina = ThinkOauth::getInstance('sina', $token); $data = $sina->call('users/show', "uid={$sina->openid()}"); if($data['error_code'] == 0){ $userInfo['type'] = 'SINA'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['screen_name']; $userInfo['head'] = $data['avatar_large']; return $userInfo; } else { throw_exception("获取新浪微博用户信息失败:{$data['error']}"); } } //登录成功,获取网易微博用户信息 public function t163($token){ $t163 = ThinkOauth::getInstance('t163', $token); $data = $t163->call('users/show'); if($data['error_code'] == 0){ $userInfo['type'] = 'T163'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['screen_name']; $userInfo['head'] = str_replace('w=48&h=48', 'w=180&h=180', $data['profile_image_url']); return $userInfo; } else { throw_exception("获取网易微博用户信息失败:{$data['error']}"); } } //登录成功,获取人人网用户信息 public function renren($token){ $renren = ThinkOauth::getInstance('renren', $token); $data = $renren->call('users.getInfo'); if(!isset($data['error_code'])){ $userInfo['type'] = 'RENREN'; $userInfo['name'] = $data[0]['name']; $userInfo['nick'] = $data[0]['name']; $userInfo['head'] = $data[0]['headurl']; return $userInfo; } else { throw_exception("获取人人网用户信息失败:{$data['error_msg']}"); } } //登录成功,获取360用户信息 public function x360($token){ $x360 = ThinkOauth::getInstance('x360', $token); $data = $x360->call('user/me'); if($data['error_code'] == 0){ $userInfo['type'] = 'X360'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['name']; $userInfo['head'] = $data['avatar']; return $userInfo; } else { throw_exception("获取360用户信息失败:{$data['error']}"); } } //登录成功,获取豆瓣用户信息 public function douban($token){ $douban = ThinkOauth::getInstance('douban', $token); $data = $douban->call('user/~me'); if(empty($data['code'])){ $userInfo['type'] = 'DOUBAN'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['name']; $userInfo['head'] = $data['avatar']; return $userInfo; } else { throw_exception("获取豆瓣用户信息失败:{$data['msg']}"); } } //登录成功,获取Github用户信息 public function github($token){ $github = ThinkOauth::getInstance('github', $token); $data = $github->call('user'); if(empty($data['code'])){ $userInfo['type'] = 'GITHUB'; $userInfo['name'] = $data['login']; $userInfo['nick'] = $data['name']; $userInfo['head'] = $data['avatar_url']; return $userInfo; } else { throw_exception("获取Github用户信息失败:{$data}"); } } //登录成功,获取Google用户信息 public function google($token){ $google = ThinkOauth::getInstance('google', $token); $data = $google->call('userinfo'); if(!empty($data['id'])){ $userInfo['type'] = 'GOOGLE'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['name']; $userInfo['head'] = $data['picture']; return $userInfo; } else { throw_exception("获取Google用户信息失败:{$data}"); } } //登录成功,获取Google用户信息 public function msn($token){ $msn = ThinkOauth::getInstance('msn', $token); $data = $msn->call('me'); if(!empty($data['id'])){ $userInfo['type'] = 'MSN'; $userInfo['name'] = $data['name']; $userInfo['nick'] = $data['name']; $userInfo['head'] = '微软暂未提供头像URL,请通过 me/picture 接口下载'; return $userInfo; } else { throw_exception("获取msn用户信息失败:{$data}"); } } //登录成功,获取点点用户信息 public function diandian($token){ $diandian = ThinkOauth::getInstance('diandian', $token); $data = $diandian->call('user/info'); if(!empty($data['meta']['status']) && $data['meta']['status'] == 200){ $userInfo['type'] = 'DIANDIAN'; $userInfo['name'] = $data['response']['name']; $userInfo['nick'] = $data['response']['name']; $userInfo['head'] = "https://api.diandian.com/v1/blog/{$data['response']['blogs'][0]['blogUuid']}/avatar/144"; return $userInfo; } else { throw_exception("获取点点用户信息失败:{$data}"); } } //登录成功,获取淘宝网用户信息 public function taobao($token){ $taobao = ThinkOauth::getInstance('taobao', $token); $fields = 'user_id,nick,sex,buyer_credit,avatar,has_shop,vip_info'; $data = $taobao->call('taobao.user.buyer.get', "fields={$fields}"); if(!empty($data['user_buyer_get_response']['user'])){ $user = $data['user_buyer_get_response']['user']; $userInfo['type'] = 'TAOBAO'; $userInfo['name'] = $user['user_id']; $userInfo['nick'] = $user['nick']; $userInfo['head'] = $user['avatar']; return $userInfo; } else { throw_exception("获取淘宝网用户信息失败:{$data['error_response']['msg']}"); } } //登录成功,获取百度用户信息 public function baidu($token){ $baidu = ThinkOauth::getInstance('baidu', $token); $data = $baidu->call('passport/users/getLoggedInUser'); if(!empty($data['uid'])){ $userInfo['type'] = 'BAIDU'; $userInfo['name'] = $data['uid']; $userInfo['nick'] = $data['uname']; $userInfo['head'] = "http://tb.himg.baidu.com/sys/portrait/item/{$data['portrait']}"; return $userInfo; } else { throw_exception("获取百度用户信息失败:{$data['error_msg']}"); } } //登录成功,获取开心网用户信息 public function kaixin($token){ $kaixin = ThinkOauth::getInstance('kaixin', $token); $data = $kaixin->call('users/me'); if(!empty($data['uid'])){ $userInfo['type'] = 'KAIXIN'; $userInfo['name'] = $data['uid']; $userInfo['nick'] = $data['name']; $userInfo['head'] = $data['logo50']; return $userInfo; } else { throw_exception("获取开心网用户信息失败:{$data['error']}"); } } //登录成功,获取搜狐用户信息 public function sohu($token){ $sohu = ThinkOauth::getInstance('sohu', $token); $data = $sohu->call('i/prv/1/user/get-basic-info'); if('success' == $data['message'] && !empty($data['data'])){ $userInfo['type'] = 'SOHU'; $userInfo['name'] = $data['data']['open_id']; $userInfo['nick'] = $data['data']['nick']; $userInfo['head'] = $data['data']['icon']; return $userInfo; } else { throw_exception("获取搜狐用户信息失败:{$data['message']}"); } } }
然后再QQ互联那边的信息编辑的底部添加协作者账号,这样的话你在本地就可以测试了。
虚拟主机改HOST文件后,会导致你访问不到正式网站,这里的话,需要注意点就好,网站本地测试的时候通过了,上线的时候可以注释或者删掉HOST文件里面的重定向的话就可以了。
代码有点多,部分参考互联网上的信息。如果有问题可以联系留言告诉我。
上一篇: 关于刷牙的问题...
下一篇: 动态更新配置文件...