基于Thinkphp3.2的qq第三方oauth认证登录扩展类
基于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
- // +----------------------------------------------------------------------
- // | Copyright (c) 2015.
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- namespace Org\Util;
- /**
- * 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="";
- 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/oauth2.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(){
- $url="https://graph.qq.com/oauth2.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
- private function getOpenID(){
- $rzt=$this->getAccessToken();
- parse_str($rzt,$data);
- $this->accessToken=$data['access_token'];
- $url="https://graph.qq.com/oauth2.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 getUsrInfo(){
- if($_GET['state'] != $_SESSION['state']){
- exit("错误代码:300001");
- }
- $this->code=$_GET['code'];
- $openid=$this->getOpenID();
- if(empty($openid)){
- return false;
- }
- $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;
- }
- //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;
- }
- }
帅的无语!