Thinkphp3.2添加QQ互联和新浪微博一键登录功能
Thinkphp3.2添加QQ互联和新浪微博一键登录功能
现在很多网站都有 QQ互联 和新浪微博 一键登录功能,国内很多php开源项目的代码都是使用thinkphp框架编写的,但是thinkphp框架如何添加QQ互联 和新浪微博 一键登录功能呢?
方法/步骤
-
一:到腾讯QQ互联上申请APPID和APPKEY。申请地址: http://connect.qq.com/ 如同,这里我们可以获取到需要跳转到的APPID和APPKEY。新浪微博的申请同理
二:在Thinkphp官网下载 Thinkphp3.2版本的框架源码
-
本地安装好Thinkphp后,找到应用下的,Common/conf/config.php文件里加上。QQ互联对应的APPID和APPKEY等QQ_AUTH配置信息:
'QQ_AUTH' => array(
'APP_ID' => '1XDXXXXX', //你的QQ互联APPID
'APP_KEY' => '2XXXXXXXXXXXXXXXXXXXXX',
'SCOPE' => 'get_user_info,get_repost_list,add_idol,add_t,del_t,add_pic_t,del_idol',
'CALLBACK' => 'http://www.baidu.com/user/oauth/callback/type/qq.html',
),
//新浪微博的配置同理一样。
-
在Common目录下建立一个文件夹api。创建文件QQConnect.class.php ,添加如下代码,这里是 我们写好的一个类:
<?php
namespace Common\Api;
class QQConnect{
/**
* 获取QQconnect Login 跳转到的地址值
* @return array 返回包含code state
*
**/
public function login($app_id, $callback, $scope){
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="
.$app_id. "&redirect_uri=" . urlencode($callback)
. "&state=" . $_SESSION['state']
. "&scope=".urlencode($scope);
//显示出登录地址
header('Location:'.$login_url);
}
/**
* 获取access_token值
* @return array 返回包含access_token,过期时间的数组
* */
private function get_token($app_id,$app_key,$code,$callback,$state){
if($state !== $_SESSION['state']){
return false;
exit();
}
$url = "https://graph.qq.com/oauth2.0/token";
$param = array(
"grant_type" => "authorization_code",
"client_id" => $app_id,
"client_secret" => $app_key,
"code" => $code,
"state" => $state,
"redirect_uri" => $callback
);
$response = $this->get_url($url, $param);
if($response == false) {
return false;
}
$params = array();
parse_str($response, $params);
return $params["access_token"];
}
/**
* 获取client_id 和 openid
* @param $access_token access_token验证码
* @return array 返回包含 openid的数组
* */
private function get_openid($access_token) {
$url = "https://graph.qq.com/oauth2.0/me";
$param = array(
"access_token" => $access_token
);
$response = $this->get_url($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;
}
/**
* 获取用户信息
* @param $client_id
* @param $access_token
* @param $openid
* @return array 用户的信息数组
* */
public function get_user_info($app_id,$token,$openid){
$url = 'https://graph.qq.com/user/get_user_info?oauth_consumer_key='.$app_id.'&access_token='.$token.'&openid='.$openid.'&format=json';
$str = $this->get_url($url);
if($str == false) {
return false;
}
$arr = json_decode($str,true);
return $arr;
}
/**
* 请求URL地址,返回callback得到返回字符串
* @param $url qq提供的api接口地址
* */
public function callback($app_id, $app_key, $callback) {
$code = $_GET['code'];
$state = $_GET['state'];
$token = $this->get_token($app_id,$app_key,$code,$callback,$state);
$openid = $this->get_openid($token);
if(!$token || !$openid) {
return false;
exit();
}
return array('openid' => $openid, 'token' => $token);
}
/*
* HTTP GET Request
*/
private function get_url($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;
}
}
/*
* HTTP POST Request
*/
private function post_url($url, $params) {
$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 );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$content = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);
if(intval($status["http_code"]) == 200) {
return $content;
} else {
return false;
}
}
}
-
在项目下创建一个User模板,然后再创建一个OauthConnect.class.php文件,用户调用 QQConnect.class.php。
OauthConnect.class.php里面加入如下,
<?php
namespace User\Controller;
use Think\Controller;
class OauthController extends Controller {
/*
* Type类型,初始化
* QQConnet WeiboConnect
*/
public function index(){
switch ($_GET['type']) {
/* QQ互联登录 */
case qq:
$app_id = C('QQ_AUTH.APP_ID');
$scope = C('QQ_AUTH.SCOPE');
$callback = C('QQ_AUTH.CALLBACK');
$sns = new \Common\Api\QQConnect;
$sns->login($app_id, $callback, $scope);
break;
/* 新浪微博登录 */
case sina:
$app_id = C('SINA_AUTH.APP_ID');
$scope = C('SINA_AUTH.SCOPE');
$callback = C('SINA_AUTH.CALLBACK');
$sns = new \Common\Api\WeiboConnect;
$sns->login($app_id, $callback, $scope);
break;
/* 默认无登录 */
default:
$this->error("无效的第三方方式",U('/user/login/index'));
break;
}
}
/*
* 互联登录返回信息
* 获取code 和 state状态,查询数据库
* */
public function callback() {
switch ($_GET['type']) {
/* 接受QQ互联登录返回值 */
case qq:
empty($_GET['code']) && $this->error("无效的第三方方式",U('/user/login/index'));
$app_id = C('QQ_AUTH.APP_ID');
$app_key = C('QQ_AUTH.APP_KEY');
$callback = C('QQ_AUTH.CALLBACK');
$qq = new \Common\Api\QQConnect;
/* callback返回openid和access_token */
$back = $qq->callback($app_id , $app_key, $callback);
//防止刷新
empty($back) && $this->error("请重新授权登录",U('/user/login/index'));
//此处省略数据库查询,查询返回的$back['openid']
break;
/* 接受新浪微博登录返回值 */
case sina:
empty($_GET['code']) && $this->error("无效的第三方方式",U('/user/login/index'));
$app_id = C('SINA_AUTH.APP_ID');
$app_key = C('SINA_AUTH.APP_KEY');
$scope = C('SINA_AUTH.SCOPE');
$callback = C('SINA_AUTH.CALLBACK');
$weibo = new \Common\Api\WeiboConnect;
/* callback返回openid和access_toke */
$back = $weibo->callback($app_id , $app_key, $callback);
empty($back) && $this->error("请重新授权登录",U('/user/login/index'));
//此处省略数据库查询,查询返回的$back['openid']
break;
/* 默认错误跳转到登录页面 */
default:
$this->error("无效的第三方方式",U('/user/login/index'));
break;
}
}
}
END