-
Notifications
You must be signed in to change notification settings - Fork 0
/
aboutmeapi.class.php
182 lines (155 loc) · 4.69 KB
/
aboutmeapi.class.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
<?php
/**
* AboutMeApi
* https://github.com/davidgrayston/about.me-php
*
* @author David Grayston
*/
class AboutMeApi {
/**
* API path format.
*/
const API_PATH_FORMAT = 'https://api.about.me/api<version><format><obj_type><action><object><type><query>';
/**
* API key set in construct.
*/
private $key;
/**
* API version.
*/
private $version = 'v2';
/**
* API format
*/
private $format = 'json';
/**
* CURL timeout.
*/
private $timeout = 2;
/**
* Construct.
*
* @param Array $params
* 'key' (required)
* 'version' (optional)
* 'format' (optional)
* 'timeout' CURL timeout seconds (optional)
* @throws Exception
*/
public function __construct($params = array()) {
// Set key - this is a required parameter
if (!isset($params['key'])) {
throw new Exception('Please specify an about.me developer key');
}
$this->key = $params['key'];
// Set version - defaults to 'v2'
if (isset($params['version'])) {
$this->version = $params['version'];
}
// Set format - defaults to 'json'
if (isset($params['format'])) {
$this->format = $params['format'];
}
// Custom timeout.
if (isset($params['timeout'])) {
$this->timeout = intval($params['timeout']);
}
}
/**
* Prefixes URL part with slash.
*
* @param String $value
*/
private function formatUrlPart($value) {
$strValue = strval($value);
return $strValue == '' ? $strValue : '/' . $strValue;
}
/**
* Fetch remote data.
*
* @param String $obj_type
* @param String $action
* @param String $object
* @param String $type
* @param Array $queryArray
*/
private function getData($obj_type, $action, $object = '', $type = '', $queryArray = array()) {
// Build query string to append to API url.
$appendQueryString = !empty($queryArray) ? '?' . http_build_query($queryArray) : '';
// API url replacements.
$replacements = array(
'<version>' => $this->formatUrlPart($this->version),
'<format>' => $this->formatUrlPart($this->format),
'<obj_type>' => $this->formatUrlPart($obj_type),
'<action>' => $this->formatUrlPart($action),
'<object>' => $this->formatUrlPart($object),
'<type>' => $this->formatUrlPart($type),
'<query>' => $appendQueryString
);
// Populate API url replacements.
$url = str_replace(array_keys($replacements), $replacements, self::API_PATH_FORMAT);
// Initialise curl request.
$ch = curl_init($url);
// Set authentication headers.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $this->key));
// CURL settings.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get JSON response.
$response = json_decode(curl_exec($ch));
// Close connection.
curl_close($ch);
// Throw Exception if request fails.
if (!isset($response->status)) {
throw new Exception('Empty response');
}
// Throw Exception if status isn't 200.
if ($response->status != 200) {
throw new Exception($response->error_message);
}
return $response;
}
/**
* Get user profile.
*
* @param String $username
* @param Boolean $extended
*/
public function userView($username, $extended = false) {
// Build query array - defaults to empty array.
$queryArray = $extended ? array('extended' => 'true') : array();
// Return API response.
return $this->getData('user', 'view', $username, '', $queryArray);
}
/**
* Fetch specified directory.
*
* @param String $type
* @param Boolean $extended
* @throws Exception
*/
public function usersViewDirectory($type, $extended = false) {
// Build query array - defaults to empty array.
$queryArray = $extended ? array('extended' => 'true') : array();
// Array of allowed types.
$allowedTypes = array('all', 'spotlight', 'featured', 'inspirational', 'team', 'founder');
// Check $type is allowed
if (!in_array($type, $allowedTypes)) {
throw new Exception('Only the following types are allowed: ' . implode(', ', $allowedTypes));
}
// Return API response.
return $this->getData('users', 'view', 'directory', $type, $queryArray);
}
/**
* Fetch random user pages.
*
* @param Boolean $extended
*/
public function usersViewRandom($extended = false) {
// Build query array - defaults to empty array.
$queryArray = $extended ? array('extended' => 'true') : array();
// Return API response.
return $this->getData('users', 'view', 'random', '', $queryArray);
}
}