>php教程 >php手册 >基于Thinkphp3.2的qq第三方oauth认证登录扩展类

基于Thinkphp3.2的qq第三方oauth认证登录扩展类

WBOY
WBOY원래의
2016-06-07 11:39:181422검색

基于Thinkphp3.2的qq第三方oauth认证登录扩展类,
由于腾讯oauth sdk写的太多,不能与thinkphp和好的结合,最终想法讲腾讯oauth sdk写成tp的扩展类
先看代码,将代码保存在/library/org/util/Qqconnect.class.php文件中
在__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。<?php <br /> // +----------------------------------------------------------------------<br> // | Copyright (c) 2015.<br> // +----------------------------------------------------------------------<br> // | Author: qiandutianxia <br> // +----------------------------------------------------------------------<br> namespace Org\Util;<br> /**<br>  *  qq第三方登录认证<br>  */<br> class Qqconnect {<br>     private static $data;<br>     //APP ID<br>     private $app_id="";<br>     //APP KEY<br>     private $app_key="";<br>     //回调地址<br>     private $callBackUrl="";<br>     //Authorization Code<br>     private $code="";<br>     //access Token<br>     private $accessToken="";<br> <br>     public function __construct(){<br>         $this->app_id="";<br>         $this->app_key="";<br>         $this->callBackUrl="";<br>         //检查用户数据<br>         if(empty($_SESSION['QC_userData'])){<br>             self::$data = array();<br>         }else{<br>             self::$data = $_SESSION['QC_userData'];<br>         }<br>     }<br> <br> <br>     //获取Authorization Code<br>     public function getAuthCode(){<br>         $url="https://graph.qq.com/oauth2.0/authorize";<br>         $param['response_type']="code";<br>         $param['client_id']=$this->app_id;<br>         $param['redirect_uri']=$this->callBackUrl;<br>         //-------生成唯一随机串防CSRF攻击<br>                 $state = md5(uniqid(rand(), TRUE));<br>                 $_SESSION['state']=$state;<br>         $param['state']=$state;<br>                 $param['scope']="get_user_info";<br>         $param =http_build_query($param,'','&');<br>         $url=$url."?".$param;<br>         header("Location:".$url);<br>     }<br> <br>     //通过Authorization Code获取Access Token<br>     private function getAccessToken(){<br>         $url="https://graph.qq.com/oauth2.0/token";<br>         $param['grant_type']="authorization_code";<br>         $param['client_id']=$this->app_id;<br>         $param['client_secret']=$this->app_key;<br>         $param['code']=$this->code;<br>         $param['redirect_uri']=$this->callBackUrl;<br>         $param =http_build_query($param,'','&');<br>         $url=$url."?".$param;<br>         return $this->getUrl($url);<br>     }<br>     //获取openid<br>     private function getOpenID(){<br>         $rzt=$this->getAccessToken();<br>         parse_str($rzt,$data);<br>         $this->accessToken=$data['access_token'];<br>         $url="https://graph.qq.com/oauth2.0/me";<br>         $param['access_token']=$this->accessToken;<br>         $param =http_build_query($param,'','&');<br>         $url=$url."?".$param;<br>         $response=$this->getUrl($url);<br>         //--------检测错误是否发生<br>         if(strpos($response, "callback") !== false){<br>             $lpos = strpos($response, "(");<br>             $rpos = strrpos($response, ")");<br>             $response = substr($response, $lpos + 1, $rpos - $lpos -1);<br>         }<br>         $user = json_decode($response);<br>         if(isset($user->error)){<br>             exit("错误代码:100007");<br>         }<br>         return $user->openid;<br>     }<br> <br>     //获取信息<br>     public function getUsrInfo(){<br>         if($_GET['state'] != $_SESSION['state']){<br>             exit("错误代码:300001");<br>         }<br>         $this->code=$_GET['code'];<br>         $openid=$this->getOpenID();<br>         if(empty($openid)){<br>             return false;<br>         }<br>         $url="https://graph.qq.com/user/get_user_info";<br>         $param['access_token']=$this->accessToken;<br>         $param['oauth_consumer_key']=$this->app_id;<br>         $param['openid']=$openid;<br>         $param =http_build_query($param,'','&');<br>         $url=$url."?".$param;<br>         $rzt=$this->getUrl($url);<br>         return $rzt;<br>     }<br>     //CURL GET<br>     private function getUrl($url){<br>         $ch = curl_init($url);<br>         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>         curl_setopt($ch, CURLOPT_TIMEOUT, 5);<br>         if (!empty($options)){<br>             curl_setopt_array($ch, $options);<br>         }<br>         $data = curl_exec($ch);<br>         curl_close($ch);<br>         return $data;<br>     }<br> <br>     <br>     //CURL POST<br>     private function postUrl($url,$post_data){<br>         $ch = curl_init();<br>         curl_setopt($ch, CURLOPT_POST, 1);<br>         curl_setopt($ch, CURLOPT_URL, $url);<br>         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);<br>         ob_start();<br>         curl_exec($ch);<br>         $result = ob_get_contents();<br>         ob_end_clean();<br>         return $result;<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.