1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace anerg\OAuth2\Gateways; |
4
|
|
|
|
5
|
|
|
use anerg\OAuth2\Connector\Gateway; |
6
|
|
|
use anerg\OAuth2\Helper\Str; |
7
|
|
|
|
8
|
|
|
class Facebook extends Gateway |
9
|
|
|
{ |
10
|
|
|
const API_BASE = 'https://graph.facebook.com/v3.1/'; |
11
|
|
|
protected $AuthorizeURL = 'https://www.facebook.com/v3.1/dialog/oauth'; |
12
|
|
|
protected $AccessTokenURL = 'https://graph.facebook.com/v3.1/oauth/access_token'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 构造函数 |
16
|
|
|
* |
17
|
|
|
* @param array $config |
18
|
|
|
*/ |
19
|
|
|
public function __construct($config = null) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($config); |
22
|
|
|
$this->clientParams(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 设置客户端请求的参数 |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
private function clientParams() |
31
|
|
|
{ |
32
|
|
|
if (isset($this->config['access_token']) && !empty($this->config['access_token'])) { |
33
|
|
|
$this->token['access_token'] = $this->config['access_token']; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 得到跳转地址 |
39
|
|
|
*/ |
40
|
|
|
public function getRedirectUrl() |
41
|
|
|
{ |
42
|
|
|
$params = [ |
43
|
|
|
'response_type' => $this->config['response_type'], |
44
|
|
|
'client_id' => $this->config['app_id'], |
45
|
|
|
'redirect_uri' => $this->config['callback'], |
46
|
|
|
'scope' => $this->config['scope'], |
47
|
|
|
'state' => $this->config['state'] ?: Str::random(), |
48
|
|
|
]; |
49
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 获取当前授权用户的openid标识 |
54
|
|
|
*/ |
55
|
|
|
public function openid() |
56
|
|
|
{ |
57
|
|
|
$userinfo = $this->userinfo(); |
58
|
|
|
return $userinfo['openid']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 获取格式化后的用户信息 |
63
|
|
|
*/ |
64
|
|
|
public function userinfo() |
65
|
|
|
{ |
66
|
|
|
$rsp = $this->userinfoRaw(); |
67
|
|
|
|
68
|
|
|
$userinfo = [ |
69
|
|
|
'openid' => $rsp['id'], |
70
|
|
|
'channel' => 'facebook', |
71
|
|
|
'nick' => $rsp['name'], |
72
|
|
|
'gender' => $this->getGender($rsp), //不一定会返回 |
73
|
|
|
'avatar' => $this->getAvatar($rsp), |
74
|
|
|
'email' => isset($rsp['email']) ? $rsp['email'] : '', |
75
|
|
|
]; |
76
|
|
|
return $userinfo; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 获取原始接口返回的用户信息 |
81
|
|
|
*/ |
82
|
|
|
public function userinfoRaw() |
83
|
|
|
{ |
84
|
|
|
$this->getToken(); |
85
|
|
|
$fields = isset($this->config['fields']) ? $this->config['fields'] : 'id,name,gender,picture.width(400)'; |
86
|
|
|
return $this->call('me', ['access_token' => $this->token['access_token'], 'fields' => $fields], 'GET'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 发起请求 |
91
|
|
|
* |
92
|
|
|
* @param string $api |
93
|
|
|
* @param array $params |
94
|
|
|
* @param string $method |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private function call($api, $params = [], $method = 'GET') |
98
|
|
|
{ |
99
|
|
|
$method = strtoupper($method); |
100
|
|
|
$request = [ |
101
|
|
|
'method' => $method, |
102
|
|
|
'uri' => self::API_BASE . $api, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
$data = $this->$method($request['uri'], $params); |
106
|
|
|
|
107
|
|
|
return json_decode($data, true); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Facebook的AccessToken请求参数 |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function accessTokenParams() |
115
|
|
|
{ |
116
|
|
|
$params = [ |
117
|
|
|
'client_id' => $this->config['app_id'], |
118
|
|
|
'client_secret' => $this->config['app_secret'], |
119
|
|
|
'code' => isset($_REQUEST['code']) ? $_REQUEST['code'] : '', |
120
|
|
|
'redirect_uri' => $this->config['callback'], |
121
|
|
|
]; |
122
|
|
|
return $params; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 解析access_token方法请求后的返回值 |
127
|
|
|
* @param string $token 获取access_token的方法的返回值 |
128
|
|
|
*/ |
129
|
|
|
protected function parseToken($token) |
130
|
|
|
{ |
131
|
|
|
$token = json_decode($token, true); |
132
|
|
|
if (isset($token['error'])) { |
133
|
|
|
throw new \Exception($token['error']['message']); |
134
|
|
|
} |
135
|
|
|
return $token; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 格式化性别 |
140
|
|
|
* |
141
|
|
|
* @param array $rsp |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
private function getGender($rsp) |
145
|
|
|
{ |
146
|
|
|
$gender = isset($rsp['gender']) ? $rsp['gender'] : null; |
147
|
|
|
$return = 'n'; |
148
|
|
|
switch ($gender) { |
149
|
|
|
case 'male': |
150
|
|
|
$return = 'm'; |
151
|
|
|
break; |
152
|
|
|
case 'female': |
153
|
|
|
$return = 'f'; |
154
|
|
|
break; |
155
|
|
|
} |
156
|
|
|
return $return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 获取用户头像 |
161
|
|
|
* |
162
|
|
|
* @param array $rsp |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
private function getAvatar($rsp) |
166
|
|
|
{ |
167
|
|
|
if (isset($rsp['picture']['data']['url'])) { |
168
|
|
|
return $rsp['picture']['data']['url']; |
169
|
|
|
} |
170
|
|
|
return ''; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|