QqOAuth.php
3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @copyright Copyright(c) 2014 http://www.webzhu.com
* @breif
* @author Tiny
* @date 2014-12-16
* @version 0.6
*/
/**
* QQ 第三方登录Oauth
*
* @author Tiny
* @package QqOAuth
*/
class QqOAuth extends OAuth{
/**
* requestCodeURL 地址
*
* @access protected
* @var string
*/
protected $requestCodeURL = 'https://graph.qq.com/oauth2.0/authorize';
/**
* access_token的URL地址
*
* @access protected
* @var string
*/
protected $accessTokenURL = 'https://graph.qq.com/oauth2.0/token';
/**
* request_code的额外参数,URL查询字符串格式
*
* @access protected
* @var string
*/
protected $authorize = 'scope=get_user_info,add_share';
/**
* API根路径URL
*
* @access protected
* @var string
*/
protected $apiBase = 'https://graph.qq.com/';
/**
* 组装接口调用参数 并调用接口
*
* @access protected
* @param mixed $api 第三方开方的API
* @param string $param 请求参数
* @param string $method 请求的方式 get/post
* @param bool $multi
* @return mixed
*/
public function call($api, $param = '', $method = 'GET', $multi = false){
/* 腾讯QQ调用公共参数 */
$params = array(
'oauth_consumer_key' => $this->appKey,
'access_token' => $this->token['access_token'],
'openid' => $this->openid(),
'format' => 'json'
);
$data = $this->http($this->url($api), $this->param($params, $param), $method);
return json_decode($data, true);
}
/**
* 解析access_token方法请求后的返回值
*
* @access protected
* @param mixed $result token返回值
* @param mixed $extend 扩展参数
* @return array
*/
protected function parsetoken($result, $extend){
parse_str($result, $data);
if($data['access_token'] && $data['expires_in']){
$this->token = $data;
$data['openid'] = $this->openid();
return $data;
} else
throw new Exception("获取腾讯QQ ACCESS_token 出错:{$result}");
}
/**
* 用户的openID
*
* @access public
* @return string
*/
public function openid(){
$data = $this->token;
if(isset($data['openid']))
return $data['openid'];
elseif($data['access_token']){
$data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token']));
$data = json_decode(trim(substr($data, 9), " );\n"), true);
if(isset($data['openid']))
return $data['openid'];
else
throw new Exception("获取用户openid出错:{$data['error_description']}");
} else {
throw new Exception('没有获取到openid!');
}
}
/**
* 获取用户信息
*
* @access public
* @return array
*/
public function getUserInfo(){
$data = $this->call('user/get_user_info');
$userInfo = array();
if(!isset($data['ret']) || $data['ret'] == 0){
$userInfo['type'] = 'QQ';
$userInfo['name'] = $data['nickname'];
$userInfo['open_name'] = $data['nickname'];
$userInfo['head'] = $data['figureurl_2'];
return $userInfo;
} else {
throw_exception("获取腾讯QQ用户信息失败:{$data['msg']}");
}
}
}