Home>

php composer.phar require --prefer-dist yiisoft/yii2-authclient "*"

quick start

Change the yii2 configuration file config/main.php and add the following 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 entry file,Generally it is app/controllers/sitecontroller.php, add code in function actions,At the same time increase the callback function successcallback, 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
 }
}

In the logged in views, add the following code

?= yii \ authclient \ widgets \ authchoice ::widget ([
 "baseauthurl" =>["site/auth"]
])?>

The above is the official documentation.Now let ’s access qq interconnect

Add qq login components I put it in common/components/qqoauth.php, the source code is as follows

<?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<[email protected]>
 * @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";
 }
 /**
 * The initial processing method of this extension seems to be unavailable for QQ interconnection.
The method should be rewritten accordingly
 * @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) {
   //The following code is specifically for QQ login,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;
         }
   //End of code addition
         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, roughly 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's information is in $attributes, the following is the code you added according to your actual situation
 //If you have qq internet login at the same time,Sina Weibo, etc.
Can be distinguished by $client->id.
}

Finally add qq login link in the login view file

<a href="/site/auth?authclient=qq">Quick login using qq</a>
php
  • Previous WebApi + Bootstrap + KnockoutJs to create a single page program
  • Next jQuery countdown effect based on extension