Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
stream.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Linkedin
5  *
6  * @copyright Copyright (C) 2005 - 2013 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 Social Stream class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Linkedin
17  * @since 13.1
18  */
20 {
21  /**
22  * Method to add a new share. Note: post must contain comment and/or (title and url).
23  *
24  * @param string $visibility One of anyone: all members or connections-only: connections only.
25  * @param string $comment Text of member's comment.
26  * @param string $title Title of shared document.
27  * @param string $url URL for shared content.
28  * @param string $image URL for image of shared content.
29  * @param string $description Description of shared content.
30  * @param boolean $twitter True to have LinkedIn pass the status message along to a member's tethered Twitter account.
31  *
32  * @return array The decoded JSON response
33  *
34  * @since 13.1
35  * @throws RuntimeException
36  */
37  public function share($visibility, $comment = null, $title = null, $url = null, $image = null, $description = null, $twitter = false)
38  {
39  $token = $this->oauth->getToken();
40 
41  // Set parameters.
42  $parameters = array(
43  'oauth_token' => $token['key']
44  );
45 
46  // Set the success response code.
47  $this->oauth->setOption('success_code', 201);
48 
49  // Set the API base
50  $base = '/v1/people/~/shares';
51 
52  // Check if twitter is true.
53  if ($twitter)
54  {
55  $base .= '?twitter-post=true';
56  }
57 
58  // Build xml.
59  $xml = '<share>
60  <visibility>
61  <code>' . $visibility . '</code>
62  </visibility>';
63 
64  // Check if comment specified.
65  if ($comment)
66  {
67  $xml .= '<comment>' . $comment . '</comment>';
68  }
69 
70  // Check if title and url are specified.
71  if ($title && $url)
72  {
73  $xml .= '<content>
74  <title>' . $title . '</title>
75  <submitted-url>' . $url . '</submitted-url>';
76 
77  // Check if image is specified.
78  if ($image)
79  {
80  $xml .= '<submitted-image-url>' . $image . '</submitted-image-url>';
81  }
82 
83  // Check if descrption id specified.
84  if ($description)
85  {
86  $xml .= '<description>' . $description . '</description>';
87  }
88 
89  $xml .= '</content>';
90  }
91  elseif (!$comment)
92  {
93  throw new RuntimeException('Post must contain comment and/or (title and url).');
94  }
95 
96  $xml .= '</share>';
97 
98  // Build the request path.
99  $path = $this->getOption('api.url') . $base;
100 
101  $header['Content-Type'] = 'text/xml';
102 
103  // Send the request.
104  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
105 
106  return $response;
107  }
108 
109  /**
110  * Method to reshare an existing share.
111  *
112  * @param string $visibility One of anyone: all members or connections-only: connections only.
113  * @param string $id The unique identifier for a share.
114  * @param string $comment Text of member's comment.
115  * @param boolean $twitter True to have LinkedIn pass the status message along to a member's tethered Twitter account.
116  *
117  * @return array The decoded JSON response
118  *
119  * @since 13.1
120  * @throws RuntimeException
121  */
122  public function reshare($visibility, $id, $comment = null, $twitter = false)
123  {
124  $token = $this->oauth->getToken();
125 
126  // Set parameters.
127  $parameters = array(
128  'oauth_token' => $token['key']
129  );
130 
131  // Set the success response code.
132  $this->oauth->setOption('success_code', 201);
133 
134  // Set the API base
135  $base = '/v1/people/~/shares';
136 
137  // Check if twitter is true.
138  if ($twitter)
139  {
140  $base .= '?twitter-post=true';
141  }
142 
143  // Build xml.
144  $xml = '<share>
145  <visibility>
146  <code>' . $visibility . '</code>
147  </visibility>';
148 
149  // Check if comment specified.
150  if ($comment)
151  {
152  $xml .= '<comment>' . $comment . '</comment>';
153  }
154 
155  $xml .= ' <attribution>
156  <share>
157  <id>' . $id . '</id>
158  </share>
159  </attribution>
160  </share>';
161 
162  // Build the request path.
163  $path = $this->getOption('api.url') . $base;
164 
165  $header['Content-Type'] = 'text/xml';
166 
167  // Send the request.
168  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
169 
170  return $response;
171  }
172 
173  /**
174  * Method to get a particular member's current share.
175  *
176  * @param string $id Member id of the profile you want.
177  * @param string $url The public profile URL.
178  *
179  * @return array The decoded JSON response
180  *
181  * @since 13.1
182  */
183  public function getCurrentShare($id = null, $url = null)
184  {
185  $token = $this->oauth->getToken();
186 
187  // Set parameters.
188  $parameters = array(
189  'oauth_token' => $token['key']
190  );
191 
192  // Set the API base
193  $base = '/v1/people/';
194 
195  // Check if a member id is specified.
196  if ($id)
197  {
198  $base .= 'id=' . $id;
199  }
200  elseif (!$url)
201  {
202  $base .= '~';
203  }
204 
205  // Check if profile url is specified.
206  if ($url)
207  {
208  $base .= 'url=' . $this->oauth->safeEncode($url);
209  }
210 
211  $base .= ':(current-share)';
212 
213  // Set request parameters.
214  $data['format'] = 'json';
215 
216  // Build the request path.
217  $path = $this->getOption('api.url') . $base;
218 
219  // Send the request.
220  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
221 
222  return json_decode($response->body);
223  }
224 
225  /**
226  * Method to get a particular member's current share.
227  *
228  * @param string $id Member id of the profile you want.
229  * @param string $url The public profile URL.
230  * @param boolean $self Used to return member's feed. Omitted to return aggregated network feed.
231  *
232  * @return array The decoded JSON response
233  *
234  * @since 13.1
235  */
236  public function getShareStream($id = null, $url = null, $self = true)
237  {
238  $token = $this->oauth->getToken();
239 
240  // Set parameters.
241  $parameters = array(
242  'oauth_token' => $token['key']
243  );
244 
245  // Set the API base
246  $base = '/v1/people/';
247 
248  // Check if a member id is specified.
249  if ($id)
250  {
251  $base .= $id;
252  }
253  elseif (!$url)
254  {
255  $base .= '~';
256  }
257 
258  // Check if profile url is specified.
259  if ($url)
260  {
261  $base .= 'url=' . $this->oauth->safeEncode($url);
262  }
263 
264  $base .= '/network';
265 
266  // Set request parameters.
267  $data['format'] = 'json';
268  $data['type'] = 'SHAR';
269 
270  // Check if self is true
271  if ($self)
272  {
273  $data['scope'] = 'self';
274  }
275 
276  // Build the request path.
277  $path = $this->getOption('api.url') . $base;
278 
279  // Send the request.
280  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
281 
282  return json_decode($response->body);
283  }
284 
285  /**
286  * Method to get the users network updates.
287  *
288  * @param string $id Member id.
289  * @param boolean $self Used to return member's feed. Omitted to return aggregated network feed.
290  * @param mixed $type String containing any valid Network Update Type from the table or an array of strings
291  * to specify more than one Network Update type.
292  * @param integer $count Number of updates to return, with a maximum of 250.
293  * @param integer $start The offset by which to start Network Update pagination.
294  * @param string $after Timestamp after which to retrieve updates.
295  * @param string $before Timestamp before which to retrieve updates.
296  * @param boolean $hidden Whether to display updates from people the member has chosen to "hide" from their update stream.
297  *
298  * @return array The decoded JSON response
299  *
300  * @since 13.1
301  */
302  public function getNetworkUpdates($id = null, $self = true, $type = null, $count = 0, $start = 0, $after = null, $before = null,
303  $hidden = false)
304  {
305  $token = $this->oauth->getToken();
306 
307  // Set parameters.
308  $parameters = array(
309  'oauth_token' => $token['key']
310  );
311 
312  // Set the API base
313  $base = '/v1/people/';
314 
315  // Check if a member id is specified.
316  if ($id)
317  {
318  $base .= $id;
319  }
320  else
321  {
322  $base .= '~';
323  }
324 
325  $base .= '/network/updates';
326 
327  // Set request parameters.
328  $data['format'] = 'json';
329 
330  // Check if self is true.
331  if ($self)
332  {
333  $data['scope'] = 'self';
334  }
335 
336  // Check if type is specified.
337  if ($type)
338  {
339  $data['type'] = $type;
340  }
341 
342  // Check if count is specified.
343  if ($count > 0)
344  {
345  $data['count'] = $count;
346  }
347 
348  // Check if start is specified.
349  if ($start > 0)
350  {
351  $data['start'] = $start;
352  }
353 
354  // Check if after is specified.
355  if ($after)
356  {
357  $data['after'] = $after;
358  }
359 
360  // Check if before is specified.
361  if ($before > 0)
362  {
363  $data['before'] = $before;
364  }
365 
366  // Check if hidden is true.
367  if ($hidden)
368  {
369  $data['hidden'] = $hidden;
370  }
371 
372  // Build the request path.
373  $path = $this->getOption('api.url') . $base;
374 
375  // Send the request.
376  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
377 
378  return json_decode($response->body);
379  }
380 
381  /**
382  * Method to get information about the current member's network.
383  *
384  * @return array The decoded JSON response
385  *
386  * @since 13.1
387  */
388  public function getNetworkStats()
389  {
390  $token = $this->oauth->getToken();
391 
392  // Set parameters.
393  $parameters = array(
394  'oauth_token' => $token['key']
395  );
396 
397  // Set the API base
398  $base = '/v1/people/~/network/network-stats';
399 
400  // Set request parameters.
401  $data['format'] = 'json';
402 
403  // Build the request path.
404  $path = $this->getOption('api.url') . $base;
405 
406  // Send the request.
407  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
408 
409  return json_decode($response->body);
410  }
411 
412  /**
413  * Method to get the users network updates.
414  *
415  * @param string $body The actual content of the update. You can use HTML to include links to the user name and the content the user
416  * created. Other HTML tags are not supported. All body text should be HTML entity escaped and UTF-8 compliant.
417  *
418  * @return array The decoded JSON response
419  *
420  * @since 13.1
421  */
422  public function postNetworkUpdate($body)
423  {
424  $token = $this->oauth->getToken();
425 
426  // Set parameters.
427  $parameters = array(
428  'oauth_token' => $token['key']
429  );
430 
431  // Set the success response code.
432  $this->oauth->setOption('success_code', 201);
433 
434  // Set the API base
435  $base = '/v1/people/~/person-activities';
436 
437  // Build the xml.
438  $xml = '<activity locale="en_US">
439  <content-type>linkedin-html</content-type>
440  <body>' . $body . '</body>
441  </activity>';
442 
443  $header['Content-Type'] = 'text/xml';
444 
445  // Build the request path.
446  $path = $this->getOption('api.url') . $base;
447 
448  // Send the request.
449  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
450 
451  return $response;
452  }
453 
454  /**
455  * Method to retrieve all comments for a given network update.
456  *
457  * @param string $key update/update-key representing an update.
458  *
459  * @return array The decoded JSON response
460  *
461  * @since 13.1
462  */
463  public function getComments($key)
464  {
465  $token = $this->oauth->getToken();
466 
467  // Set parameters.
468  $parameters = array(
469  'oauth_token' => $token['key']
470  );
471 
472  // Set the API base
473  $base = '/v1/people/~/network/updates/key=' . $key . '/update-comments';
474 
475  // Set request parameters.
476  $data['format'] = 'json';
477 
478  // Build the request path.
479  $path = $this->getOption('api.url') . $base;
480 
481  // Send the request.
482  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
483 
484  return json_decode($response->body);
485  }
486 
487  /**
488  * Method to post a new comment to an existing update.
489  *
490  * @param string $key update/update-key representing an update.
491  * @param string $comment Maximum length of 700 characters
492  *
493  * @return array The decoded JSON response
494  *
495  * @since 13.1
496  */
497  public function postComment($key, $comment)
498  {
499  $token = $this->oauth->getToken();
500 
501  // Set parameters.
502  $parameters = array(
503  'oauth_token' => $token['key']
504  );
505 
506  // Set the success response code.
507  $this->oauth->setOption('success_code', 201);
508 
509  // Set the API base
510  $base = '/v1/people/~/network/updates/key=' . $key . '/update-comments';
511 
512  // Build the xml.
513  $xml = '<update-comment>
514  <comment>' . $comment . '</comment>
515  </update-comment>';
516 
517  $header['Content-Type'] = 'text/xml';
518 
519  // Build the request path.
520  $path = $this->getOption('api.url') . $base;
521 
522  // Send the request.
523  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
524 
525  return $response;
526  }
527 
528  /**
529  * Method to retrieve the complete list of people who liked an update.
530  *
531  * @param string $key update/update-key representing an update.
532  *
533  * @return array The decoded JSON response
534  *
535  * @since 13.1
536  */
537  public function getLikes($key)
538  {
539  $token = $this->oauth->getToken();
540 
541  // Set parameters.
542  $parameters = array(
543  'oauth_token' => $token['key']
544  );
545 
546  // Set the API base
547  $base = '/v1/people/~/network/updates/key=' . $key . '/likes';
548 
549  // Set request parameters.
550  $data['format'] = 'json';
551 
552  // Build the request path.
553  $path = $this->getOption('api.url') . $base;
554 
555  // Send the request.
556  $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
557 
558  return json_decode($response->body);
559  }
560 
561  /**
562  * Method to like or unlike an update.
563  *
564  * @param string $key Update/update-key representing an update.
565  * @param boolean $like True to like update, false otherwise.
566  *
567  * @return array The decoded JSON response
568  *
569  * @since 13.1
570  */
571  private function _likeUnlike($key, $like)
572  {
573  $token = $this->oauth->getToken();
574 
575  // Set parameters.
576  $parameters = array(
577  'oauth_token' => $token['key']
578  );
579 
580  // Set the success response code.
581  $this->oauth->setOption('success_code', 204);
582 
583  // Set the API base
584  $base = '/v1/people/~/network/updates/key=' . $key . '/is-liked';
585 
586  // Build xml.
587  $xml = '<is-liked>' . $this->booleanToString($like) . '</is-liked>';
588 
589  // Build the request path.
590  $path = $this->getOption('api.url') . $base;
591 
592  $header['Content-Type'] = 'text/xml';
593 
594  // Send the request.
595  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
596 
597  return $response;
598  }
599 
600  /**
601  * Method used to like an update.
602  *
603  * @param string $key Update/update-key representing an update.
604  *
605  * @return array The decoded JSON response
606  *
607  * @since 13.1
608  */
609  public function like($key)
610  {
611  return $this->_likeUnlike($key, true);
612  }
613 
614  /**
615  * Method used to unlike an update.
616  *
617  * @param string $key Update/update-key representing an update.
618  *
619  * @return array The decoded JSON response
620  *
621  * @since 13.1
622  */
623  public function unlike($key)
624  {
625  return $this->_likeUnlike($key, false);
626  }
627 }