forked from socialadr/PHP-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialAdrAPI.php
227 lines (223 loc) · 7.13 KB
/
SocialAdrAPI.php
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
<?php
require ('SocialAdrErrors.php');
class SocialAdrAPI {
private $appId = '';
private $clientId = '';
private $clientSecret = '';
public $api = 'http://socialadr.com/api';
public $authPage = 'http://socialadr.com/pg/apps/details?id=';
public $redirectURI = ''; // The full URL to your Authorized page (redirect URI) goes here
public $scope = 'basic url account fblikes'; // The space-separated string of app permissions
public $debug = false;
public $accessToken;
public $Error;
public function __construct($clientId = null, $clientSecret = null, $appId = null) {
if (! empty($clientId)) {
$this->clientId = $clientId;
}
if (! empty($clientSecret)) {
$this->clientSecret = $clientSecret;
}
if (! empty($appId)) {
$this->appId = $appId;
}
$this->Error = new SocialAdrErrors();
}
public function getInstallURL() {
if (empty($this->appId)) {
$this->throwError('WARN', 'APP_ID_REQUIRED');
}
return $this->authPage . $this->appId;
}
public function setClientSecret($clientSecret) {
$this->clientSecret = $clientSecret;
}
public function setClientId($clientId) {
$this->clientId = $clientId;
}
public function setAppId($appId) {
$this->appId = $appId;
}
public function setAccessToken($token) {
$this->accessToken = $token;
}
public function handleAuth() {
$code = $_GET ['code'];
if (empty($code)) {
$error = $_GET ['error'];
$error_description = $_GET ['error_description'];
echo '<div>Error ' . $error . ': ' . $error_description . '</div>';
return false;
} else {
$result = $this->grant($code);
if ($result->access_token) {
$this->accessToken = $result->access_token;
}
return $result;
}
}
public function refresh($token) {
$endpoint = $this->api . '/oauth/grant';
$postData = array (
'grant_type' => 'refresh_token',
'refresh_token' => $token,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectURI,
'scope' => $this->scope //The scope of any API calls you want access to
); // The scope of any API calls you want access to
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function grant($code) {
$endpoint = $this->api . '/oauth/grant';
$postData = array (
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectURI,
'scope' => $this->scope //The scope of any API calls you want access to
); // The scope of any API calls you want access to
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function fblikesPackages() {
$this->requireAccessToken();
$endpoint = $this->api . '/oauth/fblikes/packages?access_token=' . $this->accessToken;
$result = $this->_getRequest($endpoint);
return $result;
}
public function fblikesAdd($bookmark_guid, $package_id) {
$this->requireAccessToken();
$postData = array (
'bookmark_guid' => $bookmark_guid,
'package' => $package_id
);
$endpoint = $this->api . '/oauth/fblikes/add?access_token=' . $this->accessToken;
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function urlValidate($url) {
$this->requireAccessToken();
$postData = array (
'url' => $url
);
$endpoint = $this->api . '/oauth/url/validate?access_token=' . $this->accessToken;
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function urlAdd(SocialAdrURL $url) {
$this->requireAccessToken();
$postData = array (
'url' => $url->url,
'title' => $url->title,
'descr' => $url->descr,
'tags' => $url->tags,
'category' => $url->category,
'microblog' => $url->microblog,
'submitRate' => $url->submitRate,
'submitLimit' => $url->submitLimit
);
$endpoint = $this->api . '/oauth/url/add?access_token=' . $this->accessToken;
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function urlList($limit = 100, $offset = 0, $sort = 'guid', $sort_direction = 'asc') {
$endpoint = $this->api . '/oauth/url/list?access_token=' . $this->accessToken;
$postData = array (
'limit' => $limit,
'offset' => $offset,
'sort' => $sort,
'sort_direction' => $sort_direction
);
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function reportDetail($url, $offset = 0, $limit = 10) {
$this->requireAccessToken();
$postData = array (
'url' => $url,
'offset' => $offset,
'limit' => $limit
);
$endpoint = $this->api . '/oauth/reports/detail?access_token=' . $this->accessToken;
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function reportOverview($offset = 0, $limit = 10) {
$this->requireAccessToken();
$postData = array (
'offset' => $offset,
'limit' => $limit
);
$endpoint = $this->api . '/oauth/reports/overview?access_token=' . $this->accessToken;
$result = $this->_postRequest($endpoint, $postData);
return $result;
}
public function requireAccessToken() {
if (empty($this->accessToken)) {
echo 'A valid Access Token is required to make API calls';
return false;
} else {
return true;
}
}
public function throwError($level, $error) {
$this->Error->logError($level, $error);
}
public function errors($responseType = 'html') {
return $this->Error->outputLog($responseType);
}
private function _apiCall($rType = 'GET', $endpoint, $data = null) {
$ch = curl_init();
$this->_curlOptions($ch, $rType, $endpoint, $data);
$curlResponse = curl_exec($ch);
$this->lastError = curl_error($ch);
$this->curlInfo = curl_getinfo($ch);
curl_close($ch);
if ($this->debug) {
echo '<li>Sending ' . $rType . ' Request to ' . $endpoint . '</li>';
echo 'Request Data:';
echo '<pre>' . print_r($data, true) . '</pre>';
echo 'Response JSON:';
echo '<pre>' . $curlResponse . '</pre>';
echo 'Response JSON:';
echo '<pre>' . print_r(json_decode($curlResponse)) . '</pre>';
echo 'CURL Errors:';
echo '<pre>' . $this->lastError . '</pre>';
echo '<pre>' . print_r($this->curlInfo) . '</pre>';
}
return json_decode($curlResponse);
}
private function _curlOptions($ch, $rType, $endpoint, $data) {
curl_setopt_array($ch, array (
CURLOPT_URL => $endpoint,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => count($data),
CURLOPT_POSTFIELDS => $data,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 20,
CURLOPT_PORT => 80
));
}
private function _putRequest($endpoint, $data = null) {
return $this->_apiCall('PUT', $endpoint, $data);
}
private function _postRequest($endpoint, $data = null) {
return $this->_apiCall('POST', $endpoint, $data);
}
private function _getRequest($endpoint, $data = null) {
return $this->_apiCall('GET', $endpoint, $data);
}
private function _deleteRequest($endpoint, $data = null) {
return $this->_apiCall('DELETE', $endpoint, $data);
}
}
class SocialAdrURL {
public $url, $title, $descr, $tags, $category, $microblog; // Require Parameters
// Require Parameters
public $submitLimit, $submitRate; //Optional Parameters
}