PHP、Yii PC端接QQ、微信、新浪微博 第三方登陆
来源:互联网 发布:matlab中初始化矩阵 编辑:程序博客网 时间:2024/05/19 16:04
一、QQ
1 注册成为QQ互联平台开发者,http://connect.qq.com/
2、 获取应用appID、appKey进行开发
3、 代码实例:
public function actionQqlogin()
{
$api_id = '101341668';//申请的appid
$redirect_uri = "http://www.baidu.com/user/site/qqback";//授权登陆后回调地址
$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $api_id . "&redirect_uri=" . urlencode($redirect_uri);
header('Location: '.$url);
exit;
}
//QQ回调地址
public function actionQqback()
{
$api_id = '101341668';//假的 不能用 改成自己申请的
$api_key = '289febfbfaefd979abd44e2e6c099bb7';
$redirect_uri = "http://www.baidu.com/user/site/qqback";
$code = $_REQUEST['code'];
$url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=". $api_id ."&client_secret=". $api_key ."&code=". $code ."&redirect_uri=". $redirect_uri;
$response = file_get_contents($url);//获取access_token
if(strpos($response, "callback") === false){
//成功返回, 获取到access_token, 并存入session
$params = array();
parse_str($response, $params);
$_SESSION['access_token'] = $params['access_token'];
}else{
//如果报code重复使用, 直接从session中获取
$params['access_token'] = $_SESSION['access_token'];
}
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];//获取QQ用户的信息
$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_info = json_decode($str);
if (isset($user_info->error))
{
//如有error, 跳转回首页, 此次登录失败
header('Location: /');
exit;
}
$openid = $user_info->openid;
$new_url = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=". $api_id ."&openid=". $openid;
$str = file_get_contents($new_url);
$user_info = json_decode($str);
// var_dump($user_info);
//判断当前用户是否存在
$u_info = UsersProfile::model()->find(array('condition'=>'qq_openid=:qq_openid','params'=>array('qq_openid'=>$openid)));
$connection = Yii::app()->db;
$user_info->nickname = json_encode($user_info->nickname); //暴露出unicode
$user_info->nickname = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",$user_info->nickname); //将emoji的unicode留下,其他不动
$user_info->nickname = json_decode($user_info->nickname);
$user_info->nickname = preg_replace("#(\\\ue[0-9a-f]{3})#ie","",$user_info->nickname); //将emoji的unicode留下,其他不动
$account = $user_info->nickname;
$headimg=$user_info->figureurl;
if(empty($u_info)){
Yii::app()->session['type']=1;
Yii::app()->session['account']= $user_info->nickname;
Yii::app()->session['openid']=$openid;
Yii::app()->session['headimg']=$user_info->figureurl;
$this->redirect(array('loginpost'));//跳转到登陆绑定页面
// $this->layout = false;
// $loginForm = new LoginForm;
// $this->render('binding_login', array('loginForm' => $loginForm,'account'=>$account,'openid'=> $openid,'headimg'=>$headimg,'type'=>1));
}else{
//存在此用户,直接登录
$model = Users::model()->find(array('condition'=>'uid='."'".$u_info['user_id']."'"));
$loginForm = new LoginForm;
$loginForm->username = $model['account'];
// $loginForm->password = '000000';
if($loginForm->loginMobile()){
$this->redirect(Yii::app()->request->hostInfo.Yii::app()->request->baseUrl.'/');
}else{
$this->redirect('user/users/userAuth');
}
} }
二、微信登陆
1 注册成为微信公众平台开发者,https://mp.weixin.qq.com/
2、获取应用appID进行开发
3、 代码实例:
//微信登录
public function actionWxlogin() {
$callback = 'http://www.baidu.com/user/users/wx_back'; //回调地址
//微信登录
$aoopid='wx5876816847905b8d';//微信公众平台的appid
$state = md5(uniqid(rand(), TRUE));
$_SESSION["wx_state"] = $state; //存到SESSION
$callback = urlencode($callback);
$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$aoopid."&redirect_uri=".$callback."&response_type=code&scope=snsapi_login&state=".$state."#wechat_redirect";
header("Location: $wxurl");
}
//微信登录回调地址
public function actionWx_back(){
if($_GET['state']!=$_SESSION["wx_state"]){
echo "微信登录失败!";
// header("Location: http://www.baidu.com/user/users/wx_back");
}
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.wx_appid.'&secret='.wx_appkey.'&code='.$_GET['code'].'&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
//得到 access_token 与 openid
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);
//得到 用户资料
$user_info=json_decode($json,1);
$user_info['nickname'] = json_encode($user_info['nickname']); //暴露出unicode
$user_info['nickname'] = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",$user_info['nickname']); //将emoji的unicode留下,其他不动
$user_info['nickname'] = json_decode($user_info['nickname']);
$user_info['nickname'] = preg_replace("#(\\\ue[0-9a-f]{3})#ie","",$user_info['nickname']); //将emoji的unicode留下,其他不动
$sql = "select user_id,email,nickname from users_profile where wx_openid = '".$user_info['openid']."' ";
$command = Yii::app()->db->createCommand($sql);
$result = $command->queryAll();
if($result){
// $password = '000000';
$time=time();
$email = $result[0]['email'];
// 直接登录
$model = Users::model()->find(array('condition'=>'uid='."'".$result[0]['user_id']."'"));
$loginForm = new LoginForm;
$loginForm->username = $model['account'];
// $loginForm->password = '000000';
if($loginForm->loginMobile()){
$this->redirect(Yii::app()->request->hostInfo.Yii::app()->request->baseUrl.'/');
}else{
$this->redirect('userAuth');
}
}else{
$_SESSION['type']=2;
$_SESSION['account']=$user_info['nickname'];
$_SESSION['openid']=$user_info['openid'];
$_SESSION['headimg']=$user_info['headimgurl'];
$this->redirect(array('site/loginpost'));
}
}
三、新浪微博授权登陆
1、申请APP KEY 和App Secret 地址:http://open.weibo.com/
2、代码实例
define( "WB_AKEY" , '2969301987' );//申请的appkey
define( "WB_SKEY" , 'a77a1d20d4d25f644644708c37237562' );//申请的appskey
define( "WB_CALLBACK_URL" , 'http://www.baidu.com/user/site/wbback' );//回调地址(自定义)
public function actionWblogin()
{
Yii::import('application.vendor.api.sina.*');
session_start();
include_once( 'saetv2.ex.class.php' );//引用新浪API
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
header('Location: '.$code_url);
exit;
}
......
注:新浪api文件在附件里可下载
1 0
- PHP、Yii PC端接QQ、微信、新浪微博 第三方登陆
- QQ,新浪微博第三方登陆
- PC网站微信第三方登陆
- iOS 第三方登录(QQ 微信 新浪微博)
- 友盟:微信,QQ,新浪微博第三方登录
- php 网站微信第三方登陆
- 新浪微博第三方登陆网站
- 新浪微博第三方登陆
- iOS 微信 新浪 qq 第三方登录
- android 使用sharesdk完成微信、QQ第三方登陆
- 关于使用友盟QQ微信第三方登陆
- QQ 新浪微博 淘宝账号 第三方登陆 (C#)
- QQ和新浪微博登陆第三方的简单实现
- 【QQ和新浪微博登陆第三方的简单实现】
- 第三方QQ、微博登陆
- web开发第三方登陆之新浪微博登陆
- android 调用第三方QQ、微信、新浪微博、腾讯微博等聊天方法
- iOS:shareSDK 第三方登录(qq 微信 新浪微博)
- eclipse查看某个文件的历史记录
- 解析json字符串,根据key得到对应的value的方法
- PAT-A 1092. To Buy or Not to Buy (20)
- 使用 Spring Data JPA 简化 JPA 开发
- struts2 s2-045漏洞检测 &临时解决方案
- PHP、Yii PC端接QQ、微信、新浪微博 第三方登陆
- URAL-1416 (典型的次小生成树)
- Java语言概述
- 【WebStorm】利用WebStorm来管理你的Github
- CentOS7 Firewall 简单设置
- 底部导航之BottomNavigationBar
- Theano学习笔记:“Optimization failure due to: constant_folding”错误
- 数据库
- [译]AngularJS 1.3.0 开发者指南(二) -- 概念综述