1. 程式人生 > >php QQ第三方登入/OAuth2.0驗證

php QQ第三方登入/OAuth2.0驗證

qq實現第三方網站登入

建立QQ互聯賬號

可用QQ號碼登入 登入地址:http://connect.qq.com/

QQ互聯->管理中心->建立應用

1.建立應用

2.建立網站

注意內容:網站地址一定要加入圖中所標註的資訊

建立成功

獲取 APP ID 和  APP KEY

header請求

[php] view plain copy print?
  1. publicfunction actionQQ()  
  2.     {  
  3. //$redirect 為回撥地址  $app_id 應用編號
  4.  $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='
     . $app_id . '&redirect_uri=' . $redirect;  
  5.         header('Location:' . $url);  
  6.     }  
public function actionQQ()
    {
//$redirect 為回撥地址  $app_id 應用編號
 $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' . $app_id . '&redirect_uri=' . $redirect;
        header('Location:' . $url);
    }

獲取QQ使用者資訊 [php] view plain copy print?
  1. <?php  
  2. class QQ_sdk  
  3. {  
  4.     private$app_id = '101****0572';  
  5.     private$app_secret = 'e55264******132366';  
  6.     private$redirect = 'http://www.***.cn/';  
  7.     function __construct()  
  8.     {  
  9.     }  
  10.     /** 
  11.      * [get_open_id 獲取使用者唯一ID,openid] 
  12.      * @param [string] $token [授權碼]
     
  13.      * @return [array] [成功返回client_id 和 openid ;失敗返回error 和 error_msg] 
  14.      */
  15.     function get_open_id($token)  
  16.     {  
  17.         $str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);  
  18.         if (strpos($str"callback") !== false) {  
  19.             $lpos = strpos($str"(");  
  20.             $rpos = strrpos($str")");  
  21.             $str = substr($str$lpos + 1, $rpos - $lpos - 1);  
  22.         }  
  23.         $user = json_decode($str, TRUE);  
  24.         return$user;  
  25.     }  
  26.     /** 
  27.      * [get_access_token 獲取access_token] 
  28.      * @param [string] $code [登陸後返回的$_GET['code']] 
  29.      * @return [array] [expires_in 為有效時間 , access_token 為授權碼 ; 失敗返回 error , error_description ] 
  30.      */
  31.     function get_access_token($code)  
  32.     {  
  33.         $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
  34.             . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;  
  35.         $token = array();  
  36.         parse_str($this->curl_get_content($token_url), $token);  
  37.         return$token;  
  38.     }  
  39.     /** 
  40.      * [get_user_info 獲取使用者資訊] 
  41.      * @param [string] $token [授權碼] 
  42.      * @param [string] $open_id [使用者唯一ID] 
  43.      * @return [array] [ret:返回碼,為0時成功。msg為錯誤資訊,正確返回時為空。...params] 
  44.      */
  45.     function get_user_info($token$open_id)  
  46.     {  
  47.         $user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';  
  48.         $info = json_decode($this->curl_get_content($user_info_url), TRUE);  
  49.         return$info;  
  50.     }  
  51.     privatefunction curl_get_content($url)  
  52.     {  
  53.         $ch = curl_init();  
  54.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
  55.         curl_setopt($ch, CURLOPT_URL, $url);  
  56.         //設定超時時間為3s
  57.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);  
  58.         $result = curl_exec($ch);  
  59.         curl_close($ch);  
  60.         return$result;  
  61.     }  
  62. }  
<?php

class QQ_sdk
{
    private $app_id = '101****0572';
    private $app_secret = 'e55264******132366';
    private $redirect = 'http://www.***.cn/';

    function __construct()
    {

    }

    /**
     * [get_open_id 獲取使用者唯一ID,openid]
     * @param [string] $token [授權碼]
     * @return [array] [成功返回client_id 和 openid ;失敗返回error 和 error_msg]
     */
    function get_open_id($token)
    {
        $str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
        }
        $user = json_decode($str, TRUE);
        return $user;
    }

    /**
     * [get_access_token 獲取access_token]
     * @param [string] $code [登陸後返回的$_GET['code']]
     * @return [array] [expires_in 為有效時間 , access_token 為授權碼 ; 失敗返回 error , error_description ]
     */
    function get_access_token($code)
    {
        $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
            . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;
        $token = array();
        parse_str($this->curl_get_content($token_url), $token);
        return $token;

    }

    /**
     * [get_user_info 獲取使用者資訊]
     * @param [string] $token [授權碼]
     * @param [string] $open_id [使用者唯一ID]
     * @return [array] [ret:返回碼,為0時成功。msg為錯誤資訊,正確返回時為空。...params]
     */
    function get_user_info($token, $open_id)
    {
        $user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';
        $info = json_decode($this->curl_get_content($user_info_url), TRUE);
        return $info;
    }

    private function curl_get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        //設定超時時間為3s
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

可在設定<meta property="qc:admins" content="756420************1316367" />目錄下測試

[php] view plain copy print?
  1. if(isset($_GET['code'])) {  
  2.         //qq獲取資訊
  3.          include('QQ_sdk.php');  
  4.          $qq_sdk = new Qq_sdk();   
  5.          $token = $qq_sdk->get_access_token($_GET['code']);   
  6.          echo"<pre>";  
  7.          print_r($token)."</br>";   
  8.          echo"</pre>";  
  9.              $open_id = $qq_sdk->get_open_id($token['access_token']);   
  10.          echo"<pre>";  
  11.          print_r($open_id)."</br>";   
  12.          echo"</pre>";  
  13.     }         
  14.     ?>  
if(isset($_GET['code'])) {
		//qq獲取資訊
		 include('QQ_sdk.php');
		 $qq_sdk = new Qq_sdk(); 
		 $token = $qq_sdk->get_access_token($_GET['code']); 
		 echo "<pre>";
		 print_r($token)."</br>"; 
		 echo "</pre>";
	         $open_id = $qq_sdk->get_open_id($token['access_token']); 
		 echo "<pre>";
		 print_r($open_id)."</br>"; 
		 echo "</pre>";
	}		
	?>

獲取使用者資訊