WeixinOAuth.php
5.17 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @copyright Copyright(c) 2014 http://www.webzhu.com
* @breif
* @author Tiny
* @date 2014-12-16
* @version 0.6
*/
/**
* Weixin 第三方登录Oauth
*
* @author Tiny
* @package WeixinOAuth
*/
class WeixinOAuth extends OAuth
{
/**
* requestCodeURL 地址
*
* @access protected
* @var string
*/
protected $requestCodeURL = 'https://open.weixin.qq.com/connect/qrconnect';
/**
* access_token的URL地址
*
* @access protected
* @var string
*/
protected $accessTokenURL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
/**
* request_code的额外参数,URL查询字符串格式
*
* @access protected
* @var string
*/
protected $authorize = 'scope=snsapi_login';
/**
* API根路径URL
*
* @access protected
* @var string
*/
protected $apiBase = 'https://api.weixin.qq.com/';
public function getRequestCodeURL(){
//Oauth 标准参数
$params = array(
'appid' => $this->appKey,
'redirect_uri' => $this->callBack,
'response_type' => $this->responseType,
);
//在微信里
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
if(class_exists("WeChat")){
$obj = new WeChat();
$params['appid'] = $obj->getAppID();
$this->appKey = $obj->getAppID();
$this->requestCodeURL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
$this->authorize = 'scope=snsapi_userinfo';
}
}
//获取额外参数
if($this->authorize){
parse_str($this->authorize, $_param);
if(is_array($_param)){
$params = array_merge($params, $_param);
} else {
throw new Exception('authorize配置不正确!');
}
}
return $this->requestCodeURL . '?' . http_build_query($params);
}
/**
* 组装接口调用参数 并调用接口
*
* @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){
$params = array(
'access_token' => $this->token['access_token'],
'openid' => $this->openid(),
'lang' => 'zh_CN'
);
$data = $this->http($this->url($api), $this->param($params, $param), $method);
return json_decode($data, true);
}
public function getAccessToken($code, $extend = null){
$params = array(
'appid' => $this->appKey,
'secret' => $this->appSecret,
'grant_type' => $this->grantType,
'code' => $code
);
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
$obj = new WeChat();
$params['appid'] = $obj->getAppID();
$this->appKey = $obj->getAppID();
$params['secret'] = $obj->getSecret();
$this->appSecret = $obj->getSecret();
}
$data = $this->http($this->accessTokenURL, $params, 'post');
$this->token = $this->parsetoken($data, $extend);
return $this->token;
}
/**
* 解析access_token方法请求后的返回值
*
* @access protected
* @param mixed $result token返回值
* @param mixed $extend 扩展参数
* @return array
*/
protected function parsetoken($result, $extend){
$data = JSON::decode($result);
if($data['access_token'] && $data['expires_in']){
$this->token = $data;
$data['openid'] = $this->openid();
return $data;
} else
throw new Exception("获取微信 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('sns/userinfo');
$userInfo = array();
if(!isset($data['ret']) || $data['ret'] == 0){
$userInfo['type'] = 'Weixin';
$userInfo['name'] = $data['nickname'];
$userInfo['open_name'] = $data['nickname'];
$userInfo['head'] = $data['headimgurl'];
return $userInfo;
} else {
throw_exception("获取微信用户信息失败:{$data['msg']}");
}
}
}