Call.class.php
7.01 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
define('FB_CLASS_PATH', dirname(__FILE__).'/class/');
require FB_CLASS_PATH . 'Facebook.class.php';
/**
* Facebook的调用接口
*
* @name Lib_Partner_Facebook_Call
* @package lib/partner/facebook
* @copyright yoho.inc
* @version 4.0 (2013-12-19 13:34:09)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Lib_Partner_Facebook_Call extends Lib_Partner_Factory
{
/*客户端操作对象*/
protected $client;
/*控制是否使用国外代理*/
protected $useProxy = true;
/**
* 初始化
*/
protected function init()
{
$this->client = new Facebook(array('appId' => $this->apiConfig['appId'], 'secret' => $this->apiConfig['appKey']));
}
/**
* 获取授权URL
*
* @return string
*/
public function getAuthorizeUrl()
{
return $this->client->getLoginUrl(array('redirect_uri' => $this->apiConfig['callback']));
}
/**
* 获取授权的TOKEN
*
* @return array
*/
public function getAccessToken()
{
$token = array();
if (isset($_REQUEST['code']))
{
try
{
$token['access_token'] = $this->client->getAccessToken();
$token['expires_in'] = 1393566713;
if ($token['access_token'])
{
$token['openid'] = $this->client->getUser();
}
}
catch (Exception $e)
{
// do nothing
}
}
return $token;
}
/**
* 获取当前用户的基本资料
*
* @param array $token 授权成功的TOKEN, 默认为NULL
* @return array
*/
public function getUserInfo($token)
{
$userInfo = array();
if (is_array($token) && isset($token['access_token']))
{
$fields = 'id,name,username,email,picture,gender';
try
{
if ($this->useProxy)
{
$params['accessToken'] = $token['access_token'];
$params['fields'] = $fields;
$userInfo = $this->api('me', $params);
}
else
{
$this->client->setAccessToken($token['access_token']);
$params = array('fields' => $fields,);
$userInfo = $this->client->api('/me', $params);
}
}
catch (FacebookApiException $e)
{
// do nothing
// echo $e->getMessage();
}
}
return $userInfo;
}
/**
* 获取当前用户的基本资料
*
* @param array $token 授权成功的TOKEN, 默认为NULL
* @return array
*/
public function getFriends($token, $params)
{
$friends = array();
if (is_array($token) && isset($token['access_token'], $token['openid']))
{
try
{
if ($this->useProxy)
{
$params['accessToken'] = $token['access_token'];
$name = $token['openid'] . '/friends';
$friends = $this->api($name, $params);
}
else
{
$api = '/' . $token['openid'] . '/friends';
$this->client->setAccessToken($token['access_token']);
$friends = $this->client->api($api, $params);
}
}
catch (FacebookApiException $e)
{
// do nothing
// echo $e->getMessage();
}
}
return $friends;
}
/**
* 同步分享
*
* @param array $token 授权成功的TOKEN
* @param string $content 要更新的微博信息。信息内容不超过140个汉字, 为空返回400错误。
* @param string $image 要发布的图片路径, 支持url。[只支持png/jpg/gif三种格式, 增加格式请修改get_image_mime方法]
* @param string $link URL地址, 通过该地址链接回来
* @return boolean false:失败, true:成功
*/
public function syncShare($token, $content, $image, $link)
{
$result = false;
if (is_array($token) && isset($token['access_token']))
{
try
{
if ($this->useProxy)
{
// allow: 'EVERYONE', 'ALL_FRIENDS', 'FRIENDS_OF_FRIENDS', 'CUSTOM', 'SELF'
$params = array();
$params['accessToken'] = $token['access_token'];
$params['body'] = array('message' => $content, 'picture' => $image, 'link' => $link,
'name' => '', 'caption' => '', 'description' => '', 'allow' => 'EVERYONE');
$name = $token['openid'] . '/feed';
$result = $this->api($name, $params, 'POST');
}
else
{
$params = array('method' => 'POST', 'message' => $content, 'picture' => $image, 'link' => $link, );
$api = '/' . $token['openid'] . '/feed';
$this->client->setAccessToken($token['access_token']);
$result = $this->client->api($api, $params);
}
}
catch (FacebookApiException $e)
{
// do nothing
// echo $e->getMessage();
}
}
return $result;
}
/**
* 私有的调用接口方法
*
* 备注: 因国内被禁不能直接访问FB, 所以通过此方法委托国外的服务器向FB请求,并返回结果.
*
* @param string $name 接口名称
* @param array $params 请求的参数
* @param string $method 请求方式 GET/POST
*/
private function api($name, $params = array(), $method = 'GET')
{
$url = 'http://198.11.182.44/fb';
$hander = curl_init();
$options = array(
CURLOPT_HEADER => 0,
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
);
$params['method'] = $name;
if ($method === 'GET')
{
$options[CURLOPT_URL] = $url . '?data=' . json_encode($params);
}
else
{
$params = array('data' => json_encode($params),);
$options[CURLOPT_URL] = $url;
$options[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($hander, $options);
$response = curl_exec($hander);
curl_close($hander);
$result = $response ? json_decode($response, true) : array();
return $result;
}
}