Call.php
7.07 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
244
245
246
<?php
namespace Plugin\Partner\sinaweibo;
use Plugin\Partner\Factory;
require dirname(__FILE__) . '/class/Saev2.class.php';
/**
* 新浪微博的调用接口
*
* @name Call
* @package lib/partner/sinaweibo
* @copyright yoho.inc
* @version 4.0 (2013-12-19 11:45:51)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Call extends Factory
{
/*OAuth2.0对象*/
protected $oauth;
/*客户端操作对象*/
protected $client;
/**
* 初始化
*/
protected function init()
{
$this->oauth = new \SaeTOAuthV2($this->apiConfig['appId'], $this->apiConfig['appKey']);
}
/**
* 获取授权URL
*/
public function getAuthorizeUrl()
{
return $this->oauth->getAuthorizeURL($this->apiConfig['appCallbackUrl']);
}
/**
* 获取授权的TOKEN
*/
public function getAccessToken()
{
$token = array();
if (isset($_REQUEST['code']))
{
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = $this->apiConfig['appCallbackUrl'];
try
{
$token = $this->oauth->getAccessToken('code', $keys);
}
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['uid']))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$userInfo = $this->client->show_user_by_id($token['uid']);
if (isset($userInfo['error_code']) && $userInfo['error_code'] > 0)
{
$userInfo = array();
}
}
return $userInfo;
}
/**
* 获取当前用户的关注列表
*
* @see http://open.weibo.com/wiki/2/friendships/friends
*
* @param string $token 已授权过的访问TOKEN
* @param array $params 参数列表,如下:
* @param integer $uid (要获取的用户的ID)
* @param integer count (单页返回的记录条数,默认为50,最大不超过200。)
* @param integer cursor (返回结果的游标,下一页用返回值里的next_cursor,上一页用previous_cursor,默认为0。
* @return array
*/
public function getFriends($token, $params)
{
$result = array();
if (is_array($token) && isset($token['access_token']))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$result = $this->client->friends_by_id($params['uid'], $params['cursor'], $params['count']);
if (isset($result['error_code']) && $result['error_code'] > 0)
{
$result = array();
}
}
return $result;
}
/**
* 获取用户的双向关注列表,即互粉列表
*
* @param type $token 授权成功的TOKEN
* @param type $params 参数列表
*/
public function getBilateral($token, $params)
{
$result = array();
if (is_array($token) && isset($token['access_token']))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$result = $this->client->bilateral($params['uid'], $params['page'], $params['count']);
if (isset($result['error_code']) && $result['error_code'] > 0)
{
$result = array();
}
}
return $result;
}
/**
* 同步分享
*
* @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']))
{
$content .= $link;
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$response = $this->client->upload($content, $image);
if (!isset($response['error_code']) || $response['error_code'] == 0)
{
$result = true;
}
}
return $result;
}
/**
* 加关注
*
* @param array $token 访问令牌
* @param integer $uid 需要关注的用户ID
* @param string $screenName 需要关注的用户昵称
* @return array
*/
public function follow($token, $uid = null, $screenName = null)
{
$result = array();
if (is_array($token) && isset($token['access_token']))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
if ($uid !== null)
{
$result = $this->client->follow_by_id($uid);
}
elseif ($screenName !== null)
{
$result = $this->client->follow_by_name($screenName);
}
}
return $result;
}
/**
* 发布文字微博
*
* @param array $token 访问令牌
* @param string $content 要发布的微博文本内容,必须做URLencode,内容不超过140个汉字
* @return array
*/
public function publish($token, $content)
{
$result = array();
if (is_array($token) && isset($token['access_token']) && is_string($content))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$this->client->update($content);
}
return $result;
}
/**
* 发送一条私信
*
* @param integer $uid 用户UID
* @param string $text 要发生的消息内容,文本大小必须小于300个汉字
* @param integer $id 需要发送的微博ID
* @return void
*/
public function sendMessage($token, $uid, $content, $id = null)
{
if (is_array($token) && isset($token['access_token']) && isset($uid))
{
$this->client = new SaeTClientV2($this->apiConfig['appId'], $this->apiConfig['appKey'], $token['access_token']);
$this->client->send_dm_by_id($uid, $content, $id);
}
}
}