您好,登錄后才能下訂單哦!
這篇文章主要介紹怎么以Thinkphp3.2為基礎實現QQ第三方認證登錄擴展類,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
基于Thinkphp3.2的QQ第三方認證登錄擴展類
基于Thinkphp3.2的qq第三方認證登錄擴展類,其實以下類也是我從TP官網收集整理得來的,稍微做了一下修改和完善。
這里我將文件放存在“/Application/Common/Lib/Qqconnect.class.php”。(其實這個文件路徑根據自己喜好來放)
實例化
$Qqconnect = new \Common\Lib\Qqconnect();
在__construct方法中你可以直接寫你的app_id、app_key和回調地址
也可以根據自己的喜好,改一下代碼傳參或者寫到配置文件。
調用方法:
1. 在qq的登錄按鈕的方法中調用getAuthCode方法
$qqobj=new \Org\Util\Qqconnect(); $qqobj->getAuthCode();
2.在回調地址的方法中調用getUsrInfo方法
$qqobj=new \Org\Util\Qqconnect(); $result=$qqobj->getUsrInfo();
3.getAuthCode方法中的參數scope根據自身需要添加值get_user_info,list_album,upload_pic,do_like。
Qqconnect.class.php
<?php // +---------------------------------------------------------------------- // | Copyright (c) 2015. // +---------------------------------------------------------------------- // | Author: qiandutianxia <852997402@qq.com> // +---------------------------------------------------------------------- namespace Common\Lib; /** * qq第三方登錄認證 */ class Qqconnect { private static $data; //APP ID private $app_id=""; //APP KEY private $app_key=""; //回調地址 private $callBackUrl=""; //Authorization Code private $code=""; //access Token private $accessToken=""; private $openid=""; public function __construct(){ $this->app_id=""; $this->app_key=""; $this->callBackUrl=""; //你的回調地址 //檢查用戶數據 if(empty($_SESSION['QC_userData'])){ self::$data = array(); }else{ self::$data = $_SESSION['QC_userData']; } } //獲取Authorization Code public function getAuthCode(){ $url="https://graph.qq.com/oauth3.0/authorize"; $param['response_type']="code"; $param['client_id']=$this->app_id; $param['redirect_uri']=$this->callBackUrl; //生成唯一隨機串防CSRF攻擊 $state = md5(uniqid(rand(), TRUE)); $_SESSION['state']=$state; $param['state']=$state; $param['scope']="get_user_info"; $param =http_build_query($param,'','&'); $url=$url."?".$param; header("Location:".$url); } //通過Authorization Code獲取Access Token private function _getAccessToken(){ $this->code=$_GET['code']; $url="https://graph.qq.com/oauth3.0/token"; $param['grant_type']="authorization_code"; $param['client_id']=$this->app_id; $param['client_secret']=$this->app_key; $param['code']=$this->code; $param['redirect_uri']=$this->callBackUrl; $param =http_build_query($param,'','&'); $url=$url."?".$param; return $this->getUrl($url); } //獲取openid public function _setOpenID(){ $rzt=$this->_getAccessToken(); parse_str($rzt,$data); $this->accessToken=$data['access_token']; $url="https://graph.qq.com/oauth3.0/me"; $param['access_token']=$this->accessToken; $param =http_build_query($param,'','&'); $url=$url."?".$param; $response=$this->getUrl($url); //--------檢測錯誤是否發生 if(strpos($response, "callback") !== false){ $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos -1); } $user = json_decode($response); if(isset($user->error)){ exit("錯誤代碼:100007"); } return $user->openid; } //獲取信息 public function getUserInfo(){ if($_GET['state'] != $_SESSION['state']){ exit("錯誤代碼:300001"); } $openid=$this->_setOpenID(); if(empty($openid)){ return false; } session('openid',$openid); $url="https://graph.qq.com/user/get_user_info"; $param['access_token']=$this->accessToken; $param['oauth_consumer_key']=$this->app_id; $param['openid']=$openid; $param =http_build_query($param,'','&'); $url=$url."?".$param; $rzt=$this->getUrl($url); return $rzt; } public function getOpenId(){ if($_GET['state'] != $_SESSION['state']){ exit("錯誤代碼:300001"); } $rzt=$this->_getAccessToken(); parse_str($rzt,$data); $this->accessToken=$data['access_token']; $url="https://graph.qq.com/oauth3.0/me"; $param['access_token']=$this->accessToken; $param =http_build_query($param,'','&'); $url=$url."?".$param; $response=$this->getUrl($url); //--------檢測錯誤是否發生 if(strpos($response, "callback") !== false){ $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos -1); } $info = object_array(json_decode($response)); $qq['access_token'] = $this->accessToken; $qq['openid'] = $info['openid']; session('qq',$qq); return $info['openid']; } public function getInfo($openid='',$accessToken=''){ $url="https://graph.qq.com/user/get_user_info"; $param['oauth_consumer_key']=$this->app_id; $param['access_token']=$accessToken; $param['openid']=$openid; $param =http_build_query($param,'','&'); $url=$url."?".$param; $rzt=$this->getUrl($url); $info = object_array(json_decode($rzt)); return $info; } //CURL GET private function getUrl($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (!empty($options)){ curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; } //CURL POST private function postUrl($url,$post_data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); ob_start(); curl_exec($ch); $result = ob_get_contents(); ob_end_clean(); return $result; } }
下面是控制器中的代碼
LoginController.class.php該文件主要包含兩個方式
點擊QQ登陸的時候訪問的地址
public function qq_login(){ $Qqconnect = new \Common\Lib\Qqconnect(); $Qqconnect->getAuthCode(); }
回調訪問地址
public function callback(){ $Qqconnect = new \Common\Lib\Qqconnect(); $openid = $Qqconnect->getOpenId(); $qq = session('qq'); $Member = M('Member'); $map = array(); $map['openid'] = $openid; $userInfo = $Member->where($map)->find(); if(!empty($userInfo)){ $this->success('登陸成功!',U('Member/index')); }else{ $Qqconnect = new \Common\Lib\Qqconnect(); $userInfo = $Qqconnect->getInfo($qq['openid'],$qq['access_token']); print_r($userInfo); exit; }
以上是“怎么以Thinkphp3.2為基礎實現QQ第三方認證登錄擴展類”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。