Yii2's OAuth extension and QQ login implementation method, yii2oauth. Yii2oauth describes how to implement OAuth extension and QQ internet login in yii2. For your reference, refer to OAuth extension and QQ internet login implementation in fuyii2, and yii2oauth.
This article describes how to implement OAuth extension and QQ internet login in yii2. We will share this with you for your reference. The details are as follows:
The code is as follows: php composer. phar require -- prefer-dist yiisoft/yii2-authclient "*"
Quick start
Change the configuration file config/main. php of Yii2 and add the following content to 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', ], ], ] ...]
Change the entry file, which is generally app/controllers/SiteController. php. add code in function actions and add the callback function successCallback, which is roughly as follows:
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 }}
Add the following code to the login Views:
<?= yii\authclient\widgets\AuthChoice::widget([ 'baseAuthUrl' => ['site/auth']])?>
The above is an official instruction document. next we will connect to QQ
Add the QQ login component. I am here in common/components/QqOAuth. php. the source code is as follows:
<? Phpnamespace common \ components; use yii \ authclient \ oau2; use yii \ base \ Exception; use yii \ helpers \ Json ;/****~~~ * 'Components' => [* 'authclientcollect' => [* 'class' => 'yii \ authclient \ collect ', * 'clients '=> [* 'QQ' => [* 'class' => 'common \ components \ QqOAuth', * 'clientid' => 'qq _ client_id ', * 'clientsecret' => 'qq _ client_secret', *] *... *] * ~~~ ** @ See http://connect.qq.com/ ** @ Author easypao * @ since 2.0 */class QqOAuth extends oau2{ 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 ('Oss. 2. 0/me ', 'Get'); $ qquser = $ this-> api ("user/get_user_info", 'get ', ['Oss _ consumer_key '=> $ openid ['Client _ id'], 'openid' => $ openid ['openid']); $ qquser ['openid'] = $ openid ['openid']; return $ qquser;} protec Ted function defaultName () {return 'QQ';} protected function defaultTitle () {return 'QQ';}/*** the initial processing method of this extension does not seem to be available for qq interconnection, the method * @ see \ yii \ authclient \ BaseOAuth: processResponse () */protected function processResponse ($ rawResponse, $ contentType = self: CONTENT_TYPE_AUTO) should be rewritten here) {if (empty ($ rawResponse) {return [];} switch ($ contentType) {case self: CONTENT_TYPE_AUTO: {$ contentType = $ this-> determi NeContentTypeByRaw ($ rawResponse); if ($ contentType = self: CONTENT_TYPE_AUTO) {// The following code is especially applicable to QQ internet login, it is also different from the original method 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 ;} // add end 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 ;}}
Change the config/main. php file and add it to components as follows:
'components' => [ 'authClientCollection' => [ 'class' => 'yii\authclient\Collection', 'clients' => [ 'qq' => [ 'class'=>'common\components\QqOAuth', 'clientId'=>'your_qq_clientid', 'clientSecret'=>'your_qq_secret' ], ], ]]
SiteController. php is official.
Public function successCallback ($ client) {$ attributes = $ client-> getUserAttributes (); // The user information is in $ attributes, the following code is added based on your actual situation. // If you have a logon via QQ or Sina Weibo, you can use $ client-> id to differentiate between them .}
Add the QQ logon link to the logon view file.
Use QQ to log on quickly
PS: I recommend a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat