1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Linkedin
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die();
11
12 /**
13 * Linkedin API People class for the Joomla Platform.
14 *
15 * @since 13.1
16 */
17 class JLinkedinPeople extends JLinkedinObject
18 {
19 /**
20 * Method to get a member's profile.
21 *
22 * @param string $id Member id of the profile you want.
23 * @param string $url The public profile URL.
24 * @param string $fields Request fields beyond the default ones.
25 * @param string $type Choosing public or standard profile.
26 * @param string $language A comma separated list of locales ordered from highest to lowest preference.
27 *
28 * @return array The decoded JSON response
29 *
30 * @since 13.1
31 */
32 public function getProfile($id = null, $url = null, $fields = null, $type = 'standard', $language = null)
33 {
34 $token = $this->oauth->getToken();
35
36 // Set parameters.
37 $parameters = array(
38 'oauth_token' => $token['key'],
39 );
40
41 // Set the API base
42 $base = '/v1/people/';
43
44 $data['format'] = 'json';
45
46 // Check if a member id is specified.
47 if ($id)
48 {
49 $base .= 'id=' . $id;
50 }
51 elseif (!$url)
52 {
53 $base .= '~';
54 }
55
56 // Check if profile url is specified.
57 if ($url)
58 {
59 $base .= 'url=' . $this->oauth->safeEncode($url);
60
61 // Choose public profile
62 if (!strcmp($type, 'public'))
63 {
64 $base .= ':public';
65 }
66 }
67
68 // Check if fields is specified.
69 if ($fields)
70 {
71 $base .= ':' . $fields;
72 }
73
74 // Check if language is specified.
75 $header = array();
76
77 if ($language)
78 {
79 $header = array('Accept-Language' => $language);
80 }
81
82 // Build the request path.
83 $path = $this->getOption('api.url') . $base;
84
85 // Send the request.
86 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data, $header);
87
88 return json_decode($response->body);
89 }
90
91 /**
92 * Method to get a list of connections for a user who has granted access to his/her account.
93 *
94 * @param string $fields Request fields beyond the default ones.
95 * @param integer $start Starting location within the result set for paginated returns.
96 * @param integer $count The number of results returned.
97 * @param string $modified Values are updated or new.
98 * @param string $modified_since Value as a Unix time stamp of milliseconds since epoch.
99 *
100 * @return array The decoded JSON response
101 *
102 * @since 13.1
103 */
104 public function getConnections($fields = null, $start = 0, $count = 500, $modified = null, $modified_since = null)
105 {
106 $token = $this->oauth->getToken();
107
108 // Set parameters.
109 $parameters = array(
110 'oauth_token' => $token['key'],
111 );
112
113 // Set the API base
114 $base = '/v1/people/~/connections';
115
116 $data['format'] = 'json';
117
118 // Check if fields is specified.
119 if ($fields)
120 {
121 $base .= ':' . $fields;
122 }
123
124 // Check if start is specified.
125 if ($start > 0)
126 {
127 $data['start'] = $start;
128 }
129 // Check if count is specified.
130 if ($count != 500)
131 {
132 $data['count'] = $count;
133 }
134
135 // Check if modified is specified.
136 if ($modified)
137 {
138 $data['modified'] = $modified;
139 }
140
141 // Check if modified_since is specified.
142 if ($modified_since)
143 {
144 $data['modified-since'] = $modified_since;
145 }
146
147 // Build the request path.
148 $path = $this->getOption('api.url') . $base;
149
150 // Send the request.
151 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
152
153 return json_decode($response->body);
154 }
155
156 /**
157 * Method to get information about people.
158 *
159 * @param string $fields Request fields beyond the default ones. provide 'api-standard-profile-request'
160 * field for out of network profiles.
161 * @param string $keywords Members who have all the keywords anywhere in their profile.
162 * @param string $first_name Members with a matching first name. Matches must be exact.
163 * @param string $last_name Members with a matching last name. Matches must be exactly.
164 * @param string $company_name Members who have a matching company name on their profile.
165 * @param boolean $current_company A value of true matches members who currently work at the company specified in the company-name
166 * parameter.
167 * @param string $title Matches members with that title on their profile.
168 * @param boolean $current_title A value of true matches members whose title is currently the one specified in the title-name parameter.
169 * @param string $school_name Members who have a matching school name on their profile.
170 * @param string $current_school A value of true matches members who currently attend the school specified in the school-name parameter.
171 * @param string $country_code Matches members with a location in a specific country. Values are defined in by ISO 3166 standard.
172 * Country codes must be in all lower case.
173 * @param integer $postal_code Matches members centered around a Postal Code. Must be combined with the country-code parameter.
174 * Not supported for all countries.
175 * @param integer $distance Matches members within a distance from a central point. This is measured in miles.
176 * @param string $facets Facet buckets to return, e.g. location.
177 * @param array $facet Array of facet values to search over. Contains values for location, industry, network, language,
178 * current-company, past-company and school, in exactly this order, null must be specified for an element if no value.
179 * @param integer $start Starting location within the result set for paginated returns.
180 * @param integer $count The number of results returned.
181 * @param string $sort Controls the search result order. There are four options: connections, recommenders,
182 * distance and relevance.
183 *
184 * @return array The decoded JSON response
185 *
186 * @since 13.1
187 */
188 public function search($fields = null, $keywords = null, $first_name = null, $last_name = null, $company_name = null,
189 $current_company = null, $title = null, $current_title = null, $school_name = null, $current_school = null, $country_code = null,
190 $postal_code = null, $distance = null, $facets = null, $facet = null, $start = 0, $count = 10, $sort = null)
191 {
192 $token = $this->oauth->getToken();
193
194 // Set parameters.
195 $parameters = array(
196 'oauth_token' => $token['key'],
197 );
198
199 // Set the API base
200 $base = '/v1/people-search';
201
202 $data['format'] = 'json';
203
204 // Check if fields is specified.
205 if ($fields)
206 {
207 $base .= ':' . $fields;
208 }
209
210 // Check if keywords is specified.
211 if ($keywords)
212 {
213 $data['keywords'] = $keywords;
214 }
215
216 // Check if first_name is specified.
217 if ($first_name)
218 {
219 $data['first-name'] = $first_name;
220 }
221
222 // Check if last_name is specified.
223 if ($last_name)
224 {
225 $data['last-name'] = $last_name;
226 }
227
228 // Check if company-name is specified.
229 if ($company_name)
230 {
231 $data['company-name'] = $company_name;
232 }
233
234 // Check if current_company is specified.
235 if ($current_company)
236 {
237 $data['current-company'] = $current_company;
238 }
239
240 // Check if title is specified.
241 if ($title)
242 {
243 $data['title'] = $title;
244 }
245
246 // Check if current_title is specified.
247 if ($current_title)
248 {
249 $data['current-title'] = $current_title;
250 }
251
252 // Check if school_name is specified.
253 if ($school_name)
254 {
255 $data['school-name'] = $school_name;
256 }
257
258 // Check if current_school is specified.
259 if ($current_school)
260 {
261 $data['current-school'] = $current_school;
262 }
263
264 // Check if country_code is specified.
265 if ($country_code)
266 {
267 $data['country-code'] = $country_code;
268 }
269
270 // Check if postal_code is specified.
271 if ($postal_code)
272 {
273 $data['postal-code'] = $postal_code;
274 }
275
276 // Check if distance is specified.
277 if ($distance)
278 {
279 $data['distance'] = $distance;
280 }
281
282 // Check if facets is specified.
283 if ($facets)
284 {
285 $data['facets'] = $facets;
286 }
287
288 // Check if facet is specified.
289 if ($facet)
290 {
291 $data['facet'] = array();
292
293 for ($i = 0, $iMax = count($facet); $i < $iMax; $i++)
294 {
295 if ($facet[$i])
296 {
297 if ($i == 0)
298 {
299 $data['facet'][] = 'location,' . $facet[$i];
300 }
301
302 if ($i == 1)
303 {
304 $data['facet'][] = 'industry,' . $facet[$i];
305 }
306
307 if ($i == 2)
308 {
309 $data['facet'][] = 'network,' . $facet[$i];
310 }
311
312 if ($i == 3)
313 {
314 $data['facet'][] = 'language,' . $facet[$i];
315 }
316
317 if ($i == 4)
318 {
319 $data['facet'][] = 'current-company,' . $facet[$i];
320 }
321
322 if ($i == 5)
323 {
324 $data['facet'][] = 'past-company,' . $facet[$i];
325 }
326
327 if ($i == 6)
328 {
329 $data['facet'][] = 'school,' . $facet[$i];
330 }
331 }
332 }
333 }
334
335 // Check if start is specified.
336 if ($start > 0)
337 {
338 $data['start'] = $start;
339 }
340
341 // Check if count is specified.
342 if ($count != 10)
343 {
344 $data['count'] = $count;
345 }
346
347 // Check if sort is specified.
348 if ($sort)
349 {
350 $data['sort'] = $sort;
351 }
352
353 // Build the request path.
354 $path = $this->getOption('api.url') . $base;
355
356 // Send the request.
357 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
358
359 if (strpos($fields, 'api-standard-profile-request') === false)
360 {
361 return json_decode($response->body);
362 }
363
364 // Get header name.
365 $name = explode('"name": "', $response->body);
366 $name = explode('"', $name[1]);
367 $name = $name[0];
368
369 // Get header value.
370 $value = explode('"value": "', $response->body);
371 $value = explode('"', $value[1]);
372 $value = $value[0];
373
374 // Get request url.
375 $url = explode('"url": "', $response->body);
376 $url = explode('"', $url[1]);
377 $url = $url[0];
378
379 // Build header for out of network profile.
380 $header[$name] = $value;
381
382 // Send the request.
383 $response = $this->oauth->oauthRequest($url, 'GET', $parameters, $data, $header);
384
385 return json_decode($response->body);
386 }
387 }
388