本文实例讲述了yii2中oauth扩展及qq互联登录实现方法。分享给大家供大家参考,具体如下:
quick start 快速开始
更改yii2的配置文件config/main.php,在components中增加如下内容
'components' => [ 'authclientcollection' => [ 'class' => 'yii\authclient\collection', 'clients' => [ 'google' => [ 'class' => 'yii\authclient\clients\googleopenid' ], 'facebook' => [ 'class' => 'yii\authclient\clients\facebook', 'clientid' => 'facebook_client_id', 'clientsecret' => 'facebook_client_secret', ], ], ] ... ]
更改入口文件,一般是app/controllers/sitecontroller.php,在function actions增加代码,同时增加回调函数successcallback,大致如下
class sitecontroller extends controller { public function actions() { return [ 'auth' => [ 'class' => 'yii\authclient\authaction', 'successcallback' => [$this, 'successcallback'], ], ] } public function successcallback($client) { $attributes = $client->getuserattributes(); // user login or signup comes here } }
在登录的views中,增加如下代码
<?= yii\authclient\widgets\authchoice::widget([ 'baseauthurl' => ['site/auth'] ])?>
以上是官方的说明文档,下面我们来接入qq互联
增加qq登录的组件 我这里是放在 common/components/qqoauth.php 中,源代码如下
<?php namespace common\components; use yii\authclient\oauth2; use yii\base\exception; use yii\helpers\json; /** * * ~~~ * 'components' => [ * 'authclientcollection' => [ * 'class' => 'yii\authclient\collection', * 'clients' => [ * 'qq' => [ * 'class' => 'common\components\qqoauth', * 'clientid' => 'qq_client_id', * 'clientsecret' => 'qq_client_secret', * ], * ], * ] * ... * ] * ~~~ * * @see http://connect.qq.com/ * * @author easypao <admin@easypao.com> * @since 2.0 */ class qqoauth extends oauth2 { public $authurl = 'https://graph.qq.com/oauth2.0/authorize'; public $tokenurl = 'https://graph.qq.com/oauth2.0/token'; public $apibaseurl = 'https://graph.qq.com'; public function init() { parent::init(); if ($this->scope === null) { $this->scope = implode(',', [ 'get_user_info', ]); } } protected function inituserattributes() { $openid = $this->api('oauth2.0/me', 'get'); $qquser = $this->api("user/get_user_info", 'get', ['oauth_consumer_key'=>$openid['client_id'], 'openid'=>$openid['openid']]); $qquser['openid']=$openid['openid']; return $qquser; } protected function defaultname() { return 'qq'; } protected function defaulttitle() { return 'qq'; } /** * 该扩展初始的处理方法似乎qq互联不能用,应此改写了方法 * @see \yii\authclient\baseoauth::processresponse() */ protected function processresponse($rawresponse, $contenttype = self::content_type_auto) { if (empty($rawresponse)) { return []; } switch ($contenttype) { case self::content_type_auto: { $contenttype = $this->determinecontenttypebyraw($rawresponse); if ($contenttype == self::content_type_auto) { //以下代码是特别针对qq互联登录的,也是与原方法不一样的地方 if(strpos($rawresponse, "callback") !== false){ $lpos = strpos($rawresponse, "("); $rpos = strrpos($rawresponse, ")"); $rawresponse = substr($rawresponse, $lpos + 1, $rpos - $lpos -1); $response = $this->processresponse($rawresponse, self::content_type_json); break; } //代码添加结束 throw new exception('unable to determine response content type automatically.'); } $response = $this->processresponse($rawresponse, $contenttype); break; } case self::content_type_json: { $response = json::decode($rawresponse, true); if (isset($response['error'])) { throw new exception('response error: ' . $response['error']); } break; } case self::content_type_urlencoded: { $response = []; parse_str($rawresponse, $response); break; } case self::content_type_xml: { $response = $this->convertxmltoarray($rawresponse); break; } default: { throw new exception('unknown response type "' . $contenttype . '".'); } } return $response; } }
更改 config/main.php 文件,在components中增加,大致如下
'components' => [ 'authclientcollection' => [ 'class' => 'yii\authclient\collection', 'clients' => [ 'qq' => [ 'class'=>'common\components\qqoauth', 'clientid'=>'your_qq_clientid', 'clientsecret'=>'your_qq_secret' ], ], ] ]
sitecontroller.php 就按官方那样子
public function successcallback($client) { $attributes = $client->getuserattributes(); // 用户的信息在$attributes中,以下是您根据您的实际情况增加的代码 // 如果您同时有qq互联登录,新浪微博等,可以通过 $client->id 来区别。 }
最后在登录的视图文件中 增加qq登录链接
<a href="/site/auth?authclient=qq">使用qq快速登录</a>
ps:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的php程序设计中进行代码排版:
php代码在线格式化美化工具:
更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于yii框架的php程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
php+mysql获取7天、30天的统计数据,没有数值的补充0 By勤勤学长
第一章 第六小节Duilib的WindowImplBase基类OnCreate函数中调用AttachDialog函数
开源一个Android 音视频库,包含了视频录制,视频编解码,视频编辑,常见的OpenGL操作
网友评论