Call.php
6.45 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
<?php
namespace WebPlugin\Partner\renren;
use WebPlugin\Partner\Factory;
define('RENREN_CLASS_PATH', dirname (__FILE__) . '/class/');
require RENREN_CLASS_PATH . 'RennClientBase.class.php';
/**
* 人人网的调用接口
*
* @name Call
* @package lib/partner/renren
* @copyright yoho.inc
* @version 5.0 (2015-12-31 10:54:54)
* @author xiaowei <xiaowei.gong@yoho.cn>
*/
class Call extends Factory
{
/*人人网对象*/
protected $renn;
/**
* 初始化
*/
protected function init()
{
$this->renn = new \RennClientBase($this->apiConfig['appId'], $this->apiConfig['appKey']);
}
/**
* 获取授权URL
*
* @return string
*/
public function getAuthorizeUrl()
{
return $this->renn->getAuthorizeURL($this->apiConfig['callback'], 'code', null, null, true, $this->apiConfig['scope']);
}
/**
* 获取授权的TOKEN
*
* @return array
*/
public function getAccessToken()
{
$token = array();
if (isset($_REQUEST['code']))
{
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = $this->apiConfig['callback'];
try
{
$token = $this->renn->getTokenFromTokenEndpoint('code', $keys, \TokenType::Bearer);
// 返回数组格式的信息
$token = $this->renn->getTokens();
}
catch (Exception $e)
{
// do nothing
}
}
return $token;
}
/**
* 获取当前用户的基本资料
*
* @see http://open.renren.com/wiki/API/v2/user/get
* @param object $token 授权成功的TOKEN, 默认为NULL
* @return array
*/
public function getUserInfo($token)
{
$userInfo = array();
if (!empty($token))
{
if (is_array($token))
{
$token = new \AccessToken(isset($token['token_type']) ? $token['token_type'] : \TokenType::Bearer, $token['access_token'], isset($token['refresh_token']) ? $token['refresh_token'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
}
// 获得保存的token
$this->renn->authWithToken($token);
// 获得当前登录用户
if (isset($token->accessToken))
{
$parts = explode('-', $token->accessToken);
if (isset($parts[1]))
{
$params = array('userId' => $parts[1]);
try
{
$userInfo = $this->renn->execute('/v2/user/login/get', 'GET', $params, array(), array());
}
catch (Exception $e)
{
// do nothing
}
}
}
}
return $userInfo;
}
/**
* 获取当前用户的偶像(关注)列表
*
* @see http://open.renren.com/wiki/V2/friend/list
* @param object $token 访问令牌
* @param array $params 参数列表
* Long $userId 用户ID。该字段默认为当前用户
* Integer $pageSize 页面大小。默认大小500。
* Integer $pageNumber 页码。取值大于零,默认值为1
* @return array
*/
public function getFriends($token, $params)
{
$friends = array();
if (!empty($token))
{
if (is_array($token))
{
$token = new \AccessToken(isset($token['type']) ? $token['type'] : \TokenType::Bearer, $token['accessToken'], isset($token['refreshToken']) ? $token['refreshToken'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
}
// 获得保存的token
$this->renn->authWithToken($token);
// 获取当前登录用户的好友列表
if (!isset($params['userId']) && isset($token->accessToken))
{
$parts = explode('-', $token->accessToken);
if (isset($parts[1]))
{
$params['userId'] = $parts[1];
}
}
try
{
$friends = $this->renn->execute('/v2/user/friend/list', 'GET', $params, array(), array());
}
catch (Exception $e)
{
// do nothing
}
}
return $friends;
}
/**
* 同步分享
*
* 发送自定义新鲜事。新鲜事会发布用户的个人动态信息到用户人人网主页,同时会出现在好友的新鲜事中
*
* @see http://open.renren.com/wiki/API/v2/feed/put
* @param object $token 访问令牌
* @param String $image 新鲜事图片地址
* @param String $content 新鲜事主体内容 注意:最多200个字符。
* @param String $link 新鲜事标题和图片指向的链接
* @return Long 发布新鲜事的ID
*/
public function syncShare($token, $content, $image, $link)
{
$result = false;
if (!empty($token))
{
if (is_array($token))
{
$token = new \AccessToken(isset($token['type']) ? $token['type'] : \TokenType::Bearer, $token['accessToken'], isset($token['refreshToken']) ? $token['refreshToken'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
}
// 获得保存的token
$this->renn->authWithToken($token);
$params = array('title' => '来自YOHO的分享', 'message' => $content, 'actionTargetUrl' => $link,
'imageUrl' => $image, 'description' => $content, 'targetUrl' => $link,);
try
{
$result = $this->renn->execute('/v2/feed/put', 'POST', $params, array(), array());
}
catch (Exception $e)
{
// do nothing
}
}
return $result;
}
/**
* 返回token的所有信息(包括user)
*
* 备注:所有此方法必须先调用 getAccessToken()
*
* @return array
*/
public function getTokens()
{
return $this->renn->getTokens();
}
}