php CI 第三方受權登陸 QQ受權登陸

  1. 申請地址  QQ互聯 

  2. 直接上代碼

    1. 如下代碼是放在application/libraries 若是你不是用的CI 框架也能夠直接new
    2.  
      1. QQ受權配置類  config.php
        <?php
        header('Content-Type: text/html; charset=UTF-8');
        /**
         * 	QQ 受權配置帳號信息
         */
        class Qqconfig {
        
            const QQ_APPID = '******'; //申請到的appid
            const QQ_KEY = '***********'; //申請到的appkey
        }
      2. QQ受權獲取數據類
        <?php
        
        header('Content-Type: text/html; charset=UTF-8');
        /*
         * QQ受權登陸擴展
         * 
         */
        if (!defined('BASEPATH'))
            exit('No direct script access allowed');
        include 'qqconfig.php';
        include 'qqrecorder.php';
        
        class Qqouath {
        
            const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
            const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
            const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
        
            function __construct() {
                $this->recorder = new Qqrecorder();
                $this->appid = Qqconfig::QQ_APPID;
                $this->key = Qqconfig::QQ_KEY;
            }
        
            public function qq_login($display = '', $g_ut = 1) {
        
        
               
                //-------構造請求參數列表
                $keysArr = array(
                    "response_type" => "code",
                    "client_id" => $this->appid,
                    "redirect_uri" => '',
                    "state" => '',
                    "scope" => 'get_user_info',
                    'display' => $display,
                    'g_ut' => $g_ut
                );
        
                $login_url = $this->recorder->combineURL(self::GET_AUTH_CODE_URL, $keysArr);
        
                header("Location:$login_url");
            }
        
            public function qq_callback($code = '', $return_state = '') {
                $state = $this->recorder->read("state");
        
               
        
                //-------請求參數列表
                $keysArr = array(
                    "grant_type" => "authorization_code",
                    "client_id" => $this->appid,
                    "redirect_uri" => urlencode(''),
                    "client_secret" => $this->key,
                    "code" => $code
                );
        
                //------構造請求access_token的url
                $token_url = $this->recorder->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr);
                $response = $this->recorder->get_contents($token_url);
        
                if (strpos($response, "callback") !== false) {
        
                    $lpos = strpos($response, "(");
                    $rpos = strrpos($response, ")");
                    $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
                    $msg = json_decode($response);
        
                    if (isset($msg->error)) {
                        die('獲取數據錯誤' . $msg->error_description);
                    }
                }
        
                $params = array();
                parse_str($response, $params);
                return $params["access_token"];
            }
        
            public function get_openid($access_token = '') {
        
                //-------請求參數列表
                $keysArr = array(
                    "access_token" => $access_token
                );
        
                $graph_url = $this->recorder->combineURL(self::GET_OPENID_URL, $keysArr);
                $response = $this->recorder->get_contents($graph_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)) {
                    die('數據openid錯誤:' . $user->error_description);
                }
        
                return $user->openid;
            }
        
        }
      3. QQ受權登陸 公共方法類
        <?php
        
        /* PHP SDK
         * @version 2.0.0
         * @author connect@qq.com
         * @copyright © 2013, Tencent Corporation. All rights reserved.
         */
        
        class qqrecorder {
        
            private static $data;
        
           
        
            /**
             * combineURL
             * 拼接url
             * @param string $baseURL   基於的url
             * @param array  $keysArr   參數列表數組
             * @return string           返回拼接的url
             */
            public function combineURL($baseURL, $keysArr) {
                $combined = $baseURL . "?";
                $valueArr = array();
        
                foreach ($keysArr as $key => $val) {
                    $valueArr[] = "$key=$val";
                }
        
                $keyStr = implode("&", $valueArr);
                $combined .= ($keyStr);
        
                return $combined;
            }
        
            /**
             * get_contents
             * 服務器經過get請求得到內容
             * @param string $url       請求的url,拼接後的
             * @return string           請求返回的內容
             */
            public function get_contents($url) {
                if (ini_get("allow_url_fopen") == "1") {
                    $response = file_get_contents($url);
                } else {
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    $response = curl_exec($ch);
                    curl_close($ch);
                }
        
                //-------請求爲空
                if (empty($response)) {
                    $this->error->showError("50001");
                }
        
                return $response;
            }
        
            /**
             * get
             * get方式請求資源
             * @param string $url     基於的baseUrl
             * @param array $keysArr  參數列表數組      
             * @return string         返回的資源內容
             */
            public function get($url, $keysArr) {
                $combined = $this->combineURL($url, $keysArr);
                return $this->get_contents($combined);
            }
        
            /**
             * post
             * post方式請求資源
             * @param string $url       基於的baseUrl
             * @param array $keysArr    請求的參數列表
             * @param int $flag         標誌位
             * @return string           返回的資源內容
             */
            public function post($url, $keysArr, $flag = 0) {
        
                $ch = curl_init();
                if (!$flag)
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
                curl_setopt($ch, CURLOPT_URL, $url);
                $ret = curl_exec($ch);
        
                curl_close($ch);
                return $ret;
            }
        
        }
      4. 後臺調用方法php

        class test extends CI_Controller {
            function __construct() {
                parent::__construct();
                $this->load->library('qq_login_api/qqoauth');
            }
         //QQ 登陸按鈕跳轉地址
            public function Login_For_QQ_Oauth() {
                $this->qqoauth->qq_login();
            }
        
        /*
             * QQ登陸回調地址
             * 獲取頁面返回code
             * 經過code獲取access_token
             * 經過access_token 獲取openid
             * 判斷是否存在該用戶
             */
        
            public function Login_For_QQ() {
               if (!empty($_GET['code']) ) {
                  $access_token = $this->qqoauth->qq_callback($_GET['code']);
                  if (!empty($access_token)) {
                     $openid = $this->qqoauth->get_openid($access_token);
                  }
               }
            }
        
        }
      5. 前臺調用
        window.open("test/Login_For_QQ_Oauth", "TencentLogin", "width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
相關文章
相關標籤/搜索