Qq.php
4.6 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace anerg\OAuth2\Gateways;
use anerg\OAuth2\Connector\Gateway;
class Qq extends Gateway
{
const API_BASE = 'https://graph.qq.com/';
protected $AuthorizeURL = 'https://graph.qq.com/oauth2.0/authorize';
protected $AccessTokenURL = 'https://graph.qq.com/oauth2.0/token';
/**
* 构造函数
*
* @param array $config
*/
public function __construct($config = null)
{
parent::__construct($config);
$this->clientParams();
}
/**
* 设置客户端请求的参数
*
* @return void
*/
private function clientParams()
{
if (isset($this->config['access_token']) && !empty($this->config['access_token'])) {
$this->token['access_token'] = $this->config['access_token'];
}
}
/**
* 得到跳转地址
*/
public function getRedirectUrl()
{
$params = [
'response_type' => $this->config['response_type'],
'client_id' => $this->config['app_id'],
'redirect_uri' => $this->config['callback'],
'state' => $this->config['state'],
'scope' => $this->config['scope'],
'display' => $this->display,
];
return $this->AuthorizeURL . '?' . http_build_query($params);
}
/**
* 获取当前授权用户的openid标识
*/
public function openid()
{
$this->getToken();
if (!isset($this->token['openid']) || !$this->token['openid']) {
$userID = $this->getOpenID();
$this->token['openid'] = $userID['openid'];
$this->token['unionid'] = isset($userID['unionid']) ?: '';
}
return $this->token['openid'];
}
/**
* 获取格式化后的用户信息
*/
public function userinfo()
{
$rsp = $this->userinfoRaw();
$avatar = $rsp['figureurl_qq_2'] ?: $rsp['figureurl_qq_1'];
if ($avatar) {
$avatar = \preg_replace('~\/\d+$~', '/0', $avatar);
}
$userinfo = [
'openid' => $openid = $this->openid(),
'unionid' => isset($this->token['unionid']) ? $this->token['unionid'] : '',
'channel' => 'qq',
'nick' => $rsp['nickname'],
'gender' => $rsp['gender'] == "男" ? 'm' : 'f',
'avatar' => $avatar,
];
return $userinfo;
}
/**
* 获取原始接口返回的用户信息
*/
public function userinfoRaw()
{
return $this->call('user/get_user_info');
}
/**
* 发起请求
*
* @param string $api
* @param array $params
* @param string $method
* @return array
*/
private function call($api, $params = [], $method = 'GET')
{
$method = strtoupper($method);
$params['openid'] = $this->openid();
$params['oauth_consumer_key'] = $this->config['app_id'];
$params['access_token'] = $this->token['access_token'];
$params['format'] = 'json';
$data = $this->$method(self::API_BASE . $api, $params);
$ret = json_decode($data, true);
if ($ret['ret'] != 0) {
throw new \Exception("qq获取用户信息出错:" . $ret['msg']);
}
return $ret;
}
/**
* 解析access_token方法请求后的返回值
* @param string $token 获取access_token的方法的返回值
*/
protected function parseToken($token)
{
parse_str($token, $data);
if (isset($data['access_token'])) {
return $data;
} else {
throw new \Exception("获取腾讯QQ ACCESS_TOKEN 出错:" . $token);
}
}
/**
* 通过接口获取openid
*
* @return string
*/
private function getOpenID()
{
$client = new \GuzzleHttp\Client();
$query = ['access_token' => $this->token['access_token']];
// 如果要获取unionid,先去申请:http://wiki.connect.qq.com/%E5%BC%80%E5%8F%91%E8%80%85%E5%8F%8D%E9%A6%88
if (isset($this->config['withUnionid']) && $this->config['withUnionid'] === true) {
$query['unionid'] = 1;
}
$response = $client->request('GET', self::API_BASE . 'oauth2.0/me', ['query' => $query]);
$data = $response->getBody()->getContents();
$data = json_decode(trim(substr($data, 9), " );\n"), true);
if (isset($data['openid'])) {
return $data;
} else {
throw new \Exception("获取用户openid出错:" . $data['error_description']);
}
}
}