tp5实现qq登录
qq登录
发布日期:2021-06-29 03:48:02
浏览次数:4
分类:技术文章
本文共 3678 字,大约阅读时间需要 12 分钟。
- 登录前准备
2.登录工作(五步实现qq登录)
- 放置qq图标
- 获取code
- 通过code获取token
- 通过token获取access_token再获取openid
- 通过openid获取用户全部信息
前端处理代码:
后端处理代码:
public function qqlogin() { $app_id = "101530573"; //回调地址 $redirect_uri = urlencode("http://zhangdakang.cn/index.php/index/huiyuan/token"); $url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$app_id."&redirect_uri=".$redirect_uri."&scope=get_user_info&state=text"; //跳转到$url //Step1:获取Authorization Code // header("location".$url); $this->redirect($url); } public function token() { // $code = $_GET['code']; // dump($code);exit; // appid $app_id = "101530573"; //appkey $app_secret = "bf5ad22a433ece66babedf69320ac0df"; //成功授权后的回调地址 $my_url = urlencode("http://zhangdakang.cn/index.php/index/huiyuan/token"); //获取code $code = $_GET['code']; //Step2:通过Authorization Code获取Access Token $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".$app_id."&redirect_uri=".$my_url."&client_secret=".$app_secret."&code=".$code.""; //file_get_contents() 把整个文件读入一个字符串中。 $response = file_get_contents($token_url); //Step3:在上一步获取的Access Token,得到对应用户身份的OpenID。 $params = array(); //parse_str() 函数把查询字符串('a=x&b=y')解析到变量中。 parse_str($response,$params); $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'].""; $str = file_get_contents($graph_url); // dump($str);die; // --->找到了字符串:callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} ) // // strpos() 函数查找字符串在另一字符串中第一次出现的位置,从0开始 if(strpos($str,"callback")!==false) { $lpos = strpos($str,"("); // strrpos() 函数查找字符串在另一字符串中最后一次出现的位置。 $rpos = strrpos($str,")"); //substr(string,start,length) 截取字符串某一位置 $str = substr($str,$lpos+1,$rpos-$lpos-1); } // json_decode() 函数用于对 JSON 格式-->{"a":1,"b":2,"c":3,"d":4,"e":5}<--的字符串进行解码,并转换为 PHP 变量,默认返回对象 $user = json_decode($str); // dump($user->openid);die; session('openid',$user->openid); //Step4: 调用OpenAPI接口,得到json数据,要转换为数组 $huiyuan = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=".$app_id."&openid=".$user->openid.""; //加上true,得到数组,否则默认得到对象 $res = json_decode(file_get_contents($huiyuan),true); // dump($res['nickname']);dump($res);die; $re = db('huiyuan')->where(array('openid'=>$user->openid))->find(); //如果没有找到,进行注册 if(!$re){ if($res['gender']=="男"){ $res['gender'] = 1; }else{ $res['gender'] = 2; } $data = [ 'openid'=>$user->openid, 'username'=>$res['nickname'], 'image'=>$res['figureurl_qq_2'], 'sex'=>$res['gender'], 'year'=>$res['year'], 'province'=>$res['province'], 'city'=>$res['city'], ]; $r = db('huiyuan')->insert($data); if($r){ $this->success('注册成功!',url('index/index')); }else{ $this->error('注册失败!'); } }else{ $this->redirect('login_qq'); } } public function login_qq() { $res = db('huiyuan')->where('openid',session('openid'))->find(); if(!$res){ $this->error('不是本站会员!'); }else{ session('id',$res['id']); $this->success('登录成功!','index/index'); } }
转载地址:https://blog.csdn.net/zdklhh/article/details/89375484 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月23日 15时51分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
jmeter-性能测试6-性能基础扫盲
2019-04-29
pytest+allure生成测试报告
2019-04-29
接口测试用例
2019-04-29
vnc安装配置
2019-04-29
Allure 测试报告:allure.title 去掉后方的参数化显示
2019-04-29
appium起源-环境配置
2019-04-29
appium初识-firstdemo
2019-04-29
allure报告的定制化
2019-04-29
怎样给excel添加一行数据
2019-04-29
怎样彻底帮妹子解决weditor的安装的问题
2019-04-29
干货|一次完整的性能测试,测试人员需要做什么?
2019-04-29
全栈性能测试修炼宝典
2019-04-29
jmeter-性能测试9-测试执行
2019-04-29
沟通交流碎碎念
2019-04-29
电信集成笔试题
2019-04-29
jmeter-性能测试8-性能测试基本过程及示例
2019-04-29
1.vue起步
2019-04-29
3.vue条件语句
2019-04-29
10.vue实战--form表单属性绑定
2019-04-29
11.vue实战--form表单字段验证提交验证
2019-04-29