分享QQ第三方登陆SDK

主要是考虑到QQ的PHP SDK写的真是太烂了,纯属是普及API知识,而不是到手就可以部署的类库。。反正自己都写了一个了,就拿出来分享下。。

什么也不多说,直接上代码。

Qq_sdk.php

 
 1 <?php  
 2 /**  
 3 * QQ开发平台 SDK  
 4 * 作者:偶尔陶醉  
 5 * blog: www.stutostu.com  
 6 */ 
 7  
 8 class Qq_sdk{  
 9  
10     //配置APP参数  
11     private $app_id     = 你的APP ID;  
12     private $app_secret = '你的APP_secret';  
13     private $redirect   = 你的回调地址;  
14  
15     function __construct()  
16     {  
17  
18     }  
19  
20     /**  
21      * [get_access_token  获取access_token]  
22      * @param  [string] $code [登陆后返回的$_GET['code']]  
23      * @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ]  
24      */ 
25     function get_access_token($code)  
26     {  
27         //获取access_token  
28         $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'  
29             . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect)//回调地址  
30             . '&client_secret=' . $this->app_secret . '&code=' . $code;  
31         $token =  array();  
32         //expires_in 为access_token 有效时间增量   
33         parse_str($this->_curl_get_content($token_url), $token);  
34  
35         return $token;  
36     }  
37  
38     /**  
39      * [get_open_id 获取用户唯一ID,openid]  
40      * @param  [string] $token [授权码]  
41      * @return [array] [成功返回client_id 和 openid ;失败返回error 和 error_msg]  
42      */ 
43     function get_open_id($token)  
44     {  
45         $str = $this->_curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);  
46         if (strpos($str, "callback") !== false)  
47         {  
48             $lpos = strpos($str, "(");  
49             $rpos = strrpos($str, ")");  
50             $str  = substr($str, $lpos + 1, $rpos - $lpos -1);  
51         }  
52         $user = json_decode($str, TRUE);  
53  
54         return $user;  
55     }  
56  
57     /**  
58      * [get_user_info 获取用户信息]  
59      * @param  [string] $token   [授权码]  
60      * @param  [string] $open_id [用户唯一ID]  
61      * @return [array]          [ret:返回码,为0时成功。msg为错误信息,正确返回时为空。...params]  
62      */ 
63     function get_user_info($token, $open_id)  
64     {  
65           
66         //组装URL  
67         $user_info_url = 'https://graph.qq.com/user/get_use
68 r_info?'  
69             . 'access_token=' . $token  
70             . '&oauth_consumer_key=' . $this->app_id  
71             . '&openid=' . $open_id  
72             . '&format=json';  
73  
74         $info = json_decode($this->_curl_get_content($user_info_url), TRUE);  
75  
76         return $info;  
77     }  
78  
79     private function _curl_get_content($url)  
80     {  
81         $ch = curl_init();  
82         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
83         curl_setopt($ch, CURLOPT_URL, $url);  
84         //设置超时时间为3s  
85         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);  
86         $result = curl_exec($ch);  
87         curl_close($ch);  
88  
89         return $result;  
90     }  
91  
92 }  
93  
94 /* end of Qq_sdk.php */ 

使用方法:在你网站上放置超链接,地址为:https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=你的APP_ID&redirect_uri=你的回调地址

在回调地址上调用我上面这个qq_sdk即可。

demo如下:

 
 1 if(empty($_GET['code']))  
 2 {  
 3     exit('参数非法');  
 4 }  
 5  
 6 include('qq_sdk');  
 7 $qq_sdk = new Qq_sdk();  
 8 $token = $qq_sdk->get_access_token($_GET['code']);  
 9 print_r($token);  
10  
11 $open_id = $qq_sdk->get_open_id($token['access_token']);  
12 print_r($open_id);  
13  
14  
15 $user_info = $qq_sdk->get_user_info($token['access_token'], $open_id['openid']);  
16 print_r($user_info); 
原文地址:https://www.cnblogs.com/CHEUNGKAMING/p/4080894.html