-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlinkedin.php
2102 lines (1927 loc) · 72.4 KB
/
linkedin.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file defines the 'LinkedIn' class. This class is designed to be a
* simple, stand-alone implementation of the most-used LinkedIn API functions.
*
* COPYRIGHT:
*
* Copyright (C) 2011, fiftyMission Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* SOURCE CODE LOCATION:
*
* http://code.google.com/p/simple-linkedinphp/
*
* REQUIREMENTS:
*
* 1. You must have cURL installed on the server and available to PHP.
* 2. You must be running PHP 5+.
*
* QUICK START:
*
* There are two files needed to enable LinkedIn API functionality from PHP; the
* stand-alone OAuth library, and this LinkedIn class. The latest version of
* the stand-alone OAuth library can be found on Google Code:
*
* http://code.google.com/p/oauth/
*
* Install these two files on your server in a location that is accessible to
* the scripts you wish to use them in. Make sure to change the file
* permissions such that your web server can read the files.
*
* Next, make sure the path to the OAuth library is correct (you can change this
* as needed, depending on your file organization scheme, etc).
*
* Finally, test the class by attempting to connect to LinkedIn using the
* associated demo.php page, also located at the Google Code location
* referenced above.
*
* RESOURCES:
*
* LinkedIn API Documentation from developer.linkedin.com
* Access Out-Of-Network Profiles: http://developer.linkedin.com/docs/DOC-1160
* Comments Network Updates: http://developer.linkedin.com/docs/DOC-1043
* Company Search API: http://developer.linkedin.com/docs/DOC-1325
* Connections API: http://developer.linkedin.com/docs/DOC-1004
* Experience Levels: http://developer.linkedin.com/docs/DOC-1155
* Field Selectors: http://developer.linkedin.com/docs/DOC-1014
* Get Network Updates: http://developer.linkedin.com/docs/DOC-1006
* Industry Codes: http://developer.linkedin.com/docs/DOC-1011
* Invitation API: http://developer.linkedin.com/docs/DOC-1012
* Job Bookmarks and Suggestions: http://developer.linkedin.com/docs/DOC-1323
* Job Functions: http://developer.linkedin.com/docs/DOC-1143
* Job Lookup API: http://developer.linkedin.com/docs/DOC-1322
* Job Posting API: http://developer.linkedin.com/docs/DOC-1142
* Job Search API: http://developer.linkedin.com/docs/DOC-1321
* Job Types: http://developer.linkedin.com/docs/DOC-1144
* JSON Requests & Responses: http://developer.linkedin.com/docs/DOC-1203
* Messaging API: http://developer.linkedin.com/docs/DOC-1044
* People Search API: http://developer.linkedin.com/docs/DOC-1191
* replaces Search API: http://developer.linkedin.com/docs/DOC-1005
* Profile API: http://developer.linkedin.com/docs/DOC-1002
* Profile Fields: http://developer.linkedin.com/docs/DOC-1061
* Post Network Update: http://developer.linkedin.com/docs/DOC-1009
* Share API: http://developer.linkedin.com/docs/DOC-1212
* replaces Status Update API: http://developer.linkedin.com/docs/DOC-1007
* Throttle Limits: http://developer.linkedin.com/docs/DOC-1112
* http://developer.linkedin.com/message/4626#4626
* http://developer.linkedin.com/message/3193#3193
*
* @version 3.1.1 - July 12, 2011
* @author Paul Mennega <[email protected]>
* @copyright Copyright 2011, fiftyMission Inc.
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Source: http://code.google.com/p/oauth/
*
* Rename and move as needed, changing the require_once() call to the correct
* name and path.
*/
if(!extension_loaded('oauth')) {
// the PECL OAuth extension is not present, load our third-party OAuth library
require_once('OAuth.php');
} else {
// the PECL extension is present, which is not compatible with this library
throw new LinkedInException('Simple-LinkedIn: library not compatible with installed PECL OAuth extension. Please disable this extension to use the Simple-LinkedIn library.');
}
/**
* 'LinkedInException' class declaration.
*
* This class extends the base 'Exception' class.
*
* @access public
* @package classpackage
*/
class LinkedInException extends Exception {}
/**
* 'LinkedIn' class declaration.
*
* This class provides generalized LinkedIn oauth functionality.
*
* @access public
* @package classpackage
*/
class LinkedIn {
// api/oauth settings
const _API_OAUTH_REALM = 'http://api.linkedin.com';
const _API_OAUTH_VERSION = '1.0';
// the default response format from LinkedIn
const _DEFAULT_RESPONSE_FORMAT = 'xml';
// helper constants used to standardize LinkedIn <-> API communication. See demo page for usage.
const _GET_RESPONSE = 'lResponse';
const _GET_TYPE = 'lType';
// Invitation API constants.
const _INV_SUBJECT = 'Invitation to connect';
const _INV_BODY_LENGTH = 200;
// API methods
const _METHOD_TOKENS = 'POST';
// Network API constants.
const _NETWORK_LENGTH = 1000;
const _NETWORK_HTML = '<a>';
// response format type constants, see http://developer.linkedin.com/docs/DOC-1203
const _RESPONSE_JSON = 'JSON';
const _RESPONSE_JSONP = 'JSONP';
const _RESPONSE_XML = 'XML';
// Share API constants
const _SHARE_COMMENT_LENGTH = 700;
const _SHARE_CONTENT_TITLE_LENGTH = 200;
const _SHARE_CONTENT_DESC_LENGTH = 400;
// LinkedIn API end-points
const _URL_ACCESS = 'https://api.linkedin.com/uas/oauth/accessToken';
const _URL_API = 'https://api.linkedin.com';
const _URL_AUTH = 'https://www.linkedin.com/uas/oauth/authenticate?oauth_token=';
const _URL_REQUEST = 'https://api.linkedin.com/uas/oauth/requestToken';
const _URL_REVOKE = 'https://api.linkedin.com/uas/oauth/invalidateToken';
// Library version
const _VERSION = '3.1.1';
// oauth properties
protected $callback;
protected $token = NULL;
// application properties
protected $application_key,
$application_secret;
// the format of the data to return
protected $response_format = self::_DEFAULT_RESPONSE_FORMAT;
// last request fields
public $last_request_headers,
$last_request_url;
/**
* Create a LinkedIn object, used for OAuth-based authentication and
* communication with the LinkedIn API.
*
* @param arr $config
* The 'start-up' object properties:
* - appKey => The application's API key
* - appSecret => The application's secret key
* - callbackUrl => [OPTIONAL] the callback URL
*
* @return obj
* A new LinkedIn object.
*/
public function __construct($config) {
if(!is_array($config)) {
// bad data passed
throw new LinkedInException('LinkedIn->__construct(): bad data passed, $config must be of type array.');
}
$this->setApplicationKey($config['appKey']);
$this->setApplicationSecret($config['appSecret']);
$this->setCallbackUrl($config['callbackUrl']);
}
/**
* The class destructor.
*
* Explicitly clears LinkedIn object from memory upon destruction.
*/
public function __destruct() {
unset($this);
}
/**
* Bookmark a job.
*
* Calling this method causes the current user to add a bookmark for the
* specified job:
*
* http://developer.linkedin.com/docs/DOC-1323
*
* @param str $jid
* Job ID you want to bookmark.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function bookmarkJob($jid) {
// check passed data
if(!is_string($jid)) {
// bad data passed
throw new LinkedInException('LinkedIn->bookmarkJob(): bad data passed, $jid must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/people/~/job-bookmarks';
$response = $this->fetch('POST', $query, '<job-bookmark><job><id>' . trim($jid) . '</id></job></job-bookmark>');
/**
* Check for successful request (a 201 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(201, $response);
}
/**
* Get list of jobs you have bookmarked.
*
* Returns a list of jobs the current user has bookmarked, per:
*
* http://developer.linkedin.com/docs/DOC-1323
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function bookmarkedJobs() {
// construct and send the request
$query = self::_URL_API . '/v1/people/~/job-bookmarks';
$response = $this->fetch('GET', $query);
/**
* Check for successful request (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Used to check whether a response LinkedIn object has the required http_code or not and
* returns an appropriate LinkedIn object.
*
* @param int $http_code_required
* The required http response from LinkedIn.
* @param arr $response
* An array containing a LinkedIn response.
*
* @return arr
* An array containing a LinkedIn response with appropriate values
*/
private function checkResponse($http_code_required, $response) {
// check passed data
if(!is_array($response)) {
throw new LinkedInException('LinkedIn->checkResponse(): $response must be an array');
}
if(!is_int($http_code_required)) {
throw new LinkedInException('LinkedIn->checkResponse(): $http_code_required must be an integer value');
}
// check the response
if($response['info']['http_code'] == $http_code_required) {
//request successful
$response['success'] = TRUE;
} else {
//request failed
$response['success'] = FALSE;
$response['error'] = 'HTTP response from LinkedIn end-point was not code ' . $http_code_required;
}
return $response;
}
/**
* Close a job.
*
* Calling this method causes the passed job to be closed, per:
*
* http://developer.linkedin.com/docs/DOC-1151
*
* @param str $jid
* Job ID you want to close.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function closeJob($jid) {
// check passed data
if(!is_string($jid)) {
// bad data passed
throw new LinkedInException('LinkedIn->closeJob(): bad data passed, $jid must be of string value.');
}
// construct and send the request
$query = self::_URL_API . '/v1/jobs/partner-job-id=' . trim($jid);
$response = $this->fetch('DELETE', $query);
/**
* Check for successful job close (a 204 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(204, $response);
}
/**
* Share comment posting method.
*
* Post a comment on an existing connections shared content. API details can
* be found here:
*
* http://developer.linkedin.com/docs/DOC-1043
*
* @param str $uid
* The LinkedIn update ID.
* @param str $comment
* The share comment to be posted.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function comment($uid, $comment) {
// check passed data
if(!is_string($uid)) {
// bad data passed
throw new LinkedInException('LinkedIn->comment(): bad data passed, $uid must be of type string.');
}
if(!is_string($comment)) {
// nothing/non-string passed, raise an exception
throw new LinkedInException('LinkedIn->comment(): bad data passed, $comment must be a non-zero length string.');
}
/**
* Share comment rules:
*
* 1) No HTML permitted.
* 2) Comment cannot be longer than 700 characters.
*/
$comment = substr(trim(htmlspecialchars(strip_tags($comment))), 0, self::_SHARE_COMMENT_LENGTH);
$data = '<?xml version="1.0" encoding="UTF-8"?>
<update-comment>
<comment>' . $comment . '</comment>
</update-comment>';
// construct and send the request
$query = self::_URL_API . '/v1/people/~/network/updates/key=' . $uid . '/update-comments';
$response = $this->fetch('POST', $query, $data);
/**
* Check for successful comment (a 201 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(201, $response);
}
/**
* Share comment retrieval.
*
* Return all comments associated with a given network update:
*
* http://developer.linkedin.com/docs/DOC-1043
*
* @param str $uid
* The LinkedIn update ID.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function comments($uid) {
// check passed data
if(!is_string($uid)) {
// bad data passed
throw new LinkedInException('LinkedIn->comments(): bad data passed, $uid must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/people/~/network/updates/key=' . $uid . '/update-comments';
$response = $this->fetch('GET', $query);
/**
* Check for successful comments retrieval (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Company profile retrieval function.
*
* Takes a string of parameters as input and requests company profile data
* from the LinkedIn Company Profile API. See the official documentation for
* $options 'field selector' formatting:
*
* http://developer.linkedin.com/docs/DOC-1014
* http://developer.linkedin.com/docs/DOC-1259
*
* @param str $options
* Data retrieval options.
* @param bool $by_email
* [OPTIONAL] Search by email domain?
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function company($options, $by_email = FALSE) {
// check passed data
if(!is_string($options)) {
// bad data passed
throw new LinkedInException('LinkedIn->company(): bad data passed, $options must be of type string.');
}
if(!is_bool($by_email)) {
// bad data passed
throw new LinkedInException('LinkedIn->company(): bad data passed, $by_email must be of type boolean.');
}
// construct and send the request
$query = self::_URL_API . '/v1/companies' . ($by_email ? '' : '/') . trim($options);
$response = $this->fetch('GET', $query);
/**
* Check for successful request (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Company products and their associated recommendations.
*
* The product data type contains details about a company's product or
* service, including recommendations from LinkedIn members, and replies from
* company representatives.
*
* http://developer.linkedin.com/docs/DOC-1327
*
* @param str $cid
* Company ID you want the producte for.
* @param str $options
* [OPTIONAL] Data retrieval options.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function companyProducts($cid, $options = '') {
// check passed data
if(!is_string($cid)) {
// bad data passed
throw new LinkedInException('LinkedIn->companyProducts(): bad data passed, $cid must be of type string.');
}
if(!is_string($options)) {
// bad data passed
throw new LinkedInException('LinkedIn->companyProducts(): bad data passed, $options must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/companies/' . trim($cid) . '/products' . trim($options);
$response = $this->fetch('GET', $query);
/**
* Check for successful request (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Connection retrieval function.
*
* Takes a string of parameters as input and requests connection-related data
* from the Linkedin Connections API. See the official documentation for
* $options 'field selector' formatting:
*
* http://developer.linkedin.com/docs/DOC-1014
*
* @param str $options
* [OPTIONAL] Data retrieval options.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function connections($options = '~/connections') {
// check passed data
if(!is_string($options)) {
// bad data passed
throw new LinkedInException('LinkedIn->connections(): bad data passed, $options must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/people/' . trim($options);
$response = $this->fetch('GET', $query);
/**
* Check for successful request (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Edit a job.
*
* Calling this method causes the passed job to be edited, with the passed
* XML instructing which fields to change, per:
*
* http://developer.linkedin.com/docs/DOC-1154
* http://developer.linkedin.com/docs/DOC-1142
*
* @param str $jid
* Job ID you want to renew.
* @param str $xml
* The XML containing the job fields to edit.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function editJob($jid, $xml) {
// check passed data
if(!is_string($jid)) {
// bad data passed
throw new LinkedInException('LinkedIn->editJob(): bad data passed, $jid must be of string value.');
}
if(is_string($xml)) {
$xml = trim(stripslashes($xml));
} else {
// bad data passed
throw new LinkedInException('LinkedIn->editJob(): bad data passed, $xml must be of string value.');
}
// construct and send the request
$query = self::_URL_API . '/v1/jobs/partner-job-id=' . trim($jid);
$response = $this->fetch('PUT', $query, $xml);
/**
* Check for successful job renewal (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* General data send/request method.
*
* @param str $method
* The data communication method.
* @param str $url
* The Linkedin API endpoint to connect with.
* @param str $data
* [OPTIONAL] The data to send to LinkedIn.
* @param arr $parameters
* [OPTIONAL] Addition OAuth parameters to send to LinkedIn.
*
* @return arr
* Array containing:
*
* array(
* 'info' => Connection information,
* 'linkedin' => LinkedIn response,
* 'oauth' => The OAuth request string that was sent to LinkedIn
* )
*/
protected function fetch($method, $url, $data = NULL, $parameters = array()) {
// check for cURL
if(!extension_loaded('curl')) {
// cURL not present
throw new LinkedInException('LinkedIn->fetch(): PHP cURL extension does not appear to be loaded/present.');
}
try {
// generate OAuth values
$oauth_consumer = new OAuthConsumer($this->getApplicationKey(), $this->getApplicationSecret(), $this->getCallbackUrl());
$oauth_token = $this->getToken();
$oauth_token = (!is_null($oauth_token)) ? new OAuthToken($oauth_token['oauth_token'], $oauth_token['oauth_token_secret']) : NULL;
$defaults = array(
'oauth_version' => self::_API_OAUTH_VERSION
);
$parameters = array_merge($defaults, $parameters);
// generate OAuth request
$oauth_req = OAuthRequest::from_consumer_and_token($oauth_consumer, $oauth_token, $method, $url, $parameters);
$oauth_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $oauth_consumer, $oauth_token);
// start cURL, checking for a successful initiation
if(!$handle = curl_init()) {
// cURL failed to start
throw new LinkedInException('LinkedIn->fetch(): cURL did not initialize properly.');
}
// set cURL options, based on parameters passed
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
// configure the header we are sending to LinkedIn - http://developer.linkedin.com/docs/DOC-1203
$header = array($oauth_req->to_header(self::_API_OAUTH_REALM));
if(is_null($data)) {
// not sending data, identify the content type
$header[] = 'Content-Type: text/plain; charset=UTF-8';
switch($this->getResponseFormat()) {
case self::_RESPONSE_JSON:
$header[] = 'x-li-format: json';
break;
case self::_RESPONSE_JSONP:
$header[] = 'x-li-format: jsonp';
break;
}
} else {
$header[] = 'Content-Type: text/xml; charset=UTF-8';
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
// set the last url, headers
$this->last_request_url = $url;
$this->last_request_headers = $header;
// gather the response
$return_data['linkedin'] = curl_exec($handle);
$return_data['info'] = curl_getinfo($handle);
$return_data['oauth']['header'] = $oauth_req->to_header(self::_API_OAUTH_REALM);
$return_data['oauth']['string'] = $oauth_req->base_string;
// check for throttling
if(self::isThrottled($return_data['linkedin'])) {
throw new LinkedInException('LinkedIn->fetch(): throttling limit for this user/application has been reached for LinkedIn resource - ' . $url);
}
//TODO - add check for NO response (http_code = 0) from cURL
// close cURL connection
curl_close($handle);
// no exceptions thrown, return the data
return $return_data;
} catch(OAuthException $e) {
// oauth exception raised
throw new LinkedInException('OAuth exception caught: ' . $e->getMessage());
}
}
/**
* Follow a company.
*
* Calling this method causes the current user to start following the
* specified company, per:
*
* http://developer.linkedin.com/docs/DOC-1324
*
* @param str $cid
* Company ID you want to follow.
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function followCompany($cid) {
// check passed data
if(!is_string($cid)) {
// bad data passed
throw new LinkedInException('LinkedIn->followCompany(): bad data passed, $cid must be of type string.');
}
// construct and send the request
$query = self::_URL_API . '/v1/people/~/following/companies';
$response = $this->fetch('POST', $query, '<company><id>' . trim($cid) . '</id></company>');
/**
* Check for successful request (a 201 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(201, $response);
}
/**
* Get list of companies you follow.
*
* Returns a list of companies the current user is currently following, per:
*
* http://developer.linkedin.com/docs/DOC-1324
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function followedCompanies() {
// construct and send the request
$query = self::_URL_API . '/v1/people/~/following/companies';
$response = $this->fetch('GET', $query);
/**
* Check for successful request (a 200 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(200, $response);
}
/**
* Get the application_key property.
*
* @return str
* The application key.
*/
public function getApplicationKey() {
return $this->application_key;
}
/**
* Get the application_secret property.
*
* @return str
* The application secret.
*/
public function getApplicationSecret() {
return $this->application_secret;
}
/**
* Get the callback property.
*
* @return str
* The callback url.
*/
public function getCallbackUrl() {
return $this->callback;
}
/**
* Get the response_format property.
*
* @return str
* The response format.
*/
public function getResponseFormat() {
return $this->response_format;
}
/**
* Get the token_access property.
*
* @return arr
* The access token.
*/
public function getToken() {
return $this->token;
}
/**
* [DEPRECATED] Get the token_access property.
*
* @return arr
* The access token.
*/
public function getTokenAccess() {
return $this->getToken();
}
/**
* Send connection invitations.
*
* Send an invitation to connect to your network, either by email address or
* by LinkedIn ID. Details on the API here:
*
* http://developer.linkedin.com/docs/DOC-1012
*
* @param str $method
* The invitation method to process.
* @param str $recipient
* The email/id to send the invitation to.
* @param str $subject
* The subject of the invitation to send.
* @param str $body
* The body of the invitation to send.
* @param str $type
* [OPTIONAL] The invitation request type (only friend is supported at this time by the Invite API).
*
* @return arr
* Array containing retrieval success, LinkedIn response.
*/
public function invite($method, $recipient, $subject, $body, $type = 'friend') {
/**
* Clean up the passed data per these rules:
*
* 1) Message must be sent to one recipient (only a single recipient permitted for the Invitation API)
* 2) No HTML permitted
* 3) 200 characters max in the invitation subject
* 4) Only able to connect as a friend at this point
*/
// check passed data
if(empty($recipient)) {
throw new LinkedInException('LinkedIn->invite(): you must provide an invitation recipient.');
}
switch($method) {
case 'email':
if(is_array($recipient)) {
$recipient = array_map('trim', $recipient);
} else {
// bad format for recipient for email method
throw new LinkedInException('LinkedIn->invite(): invitation recipient email/name array is malformed.');
}
break;
case 'id':
$recipient = trim($recipient);
if(!self::isId($recipient)) {
// bad format for recipient for id method
throw new LinkedInException('LinkedIn->invite(): invitation recipient ID does not match LinkedIn format.');
}
break;
default:
throw new LinkedInException('LinkedIn->invite(): bad invitation method, must be one of: email, id.');
break;
}
if(!empty($subject)) {
$subject = trim(htmlspecialchars(strip_tags(stripslashes($subject))));
} else {
throw new LinkedInException('LinkedIn->invite(): message subject is empty.');
}
if(!empty($body)) {
$body = trim(htmlspecialchars(strip_tags(stripslashes($body))));
if(strlen($body) > self::_INV_BODY_LENGTH) {
throw new LinkedInException('LinkedIn->invite(): message body length is too long - max length is ' . self::_INV_BODY_LENGTH . ' characters.');
}
} else {
throw new LinkedInException('LinkedIn->invite(): message body is empty.');
}
switch($type) {
case 'friend':
break;
default:
throw new LinkedInException('LinkedIn->invite(): bad invitation type, must be one of: friend.');
break;
}
// construct the xml data
$data = '<?xml version="1.0" encoding="UTF-8"?>
<mailbox-item>
<recipients>
<recipient>';
switch($method) {
case 'email':
// email-based invitation
$data .= '<person path="/people/email=' . $recipient['email'] . '">
<first-name>' . htmlspecialchars($recipient['first-name']) . '</first-name>
<last-name>' . htmlspecialchars($recipient['last-name']) . '</last-name>
</person>';
break;
case 'id':
// id-based invitation
$data .= '<person path="/people/id=' . $recipient . '"/>';
break;
}
$data .= ' </recipient>
</recipients>
<subject>' . $subject . '</subject>
<body>' . $body . '</body>
<item-content>
<invitation-request>
<connect-type>';
switch($type) {
case 'friend':
$data .= 'friend';
break;
}
$data .= ' </connect-type>';
switch($method) {
case 'id':
// id-based invitation, we need to get the authorization information
$query = 'id=' . $recipient . ':(api-standard-profile-request)';
$response = self::profile($query);
if($response['info']['http_code'] == 200) {
$response['linkedin'] = self::xmlToArray($response['linkedin']);
if($response['linkedin'] === FALSE) {
// bad XML data
throw new LinkedInException('LinkedIn->invite(): LinkedIn returned bad XML data.');
}
$authentication = explode(':', $response['linkedin']['person']['children']['api-standard-profile-request']['children']['headers']['children']['http-header']['children']['value']['content']);
// complete the xml
$data .= '<authorization>
<name>' . $authentication[0] . '</name>
<value>' . $authentication[1] . '</value>
</authorization>';
} else {
// bad response from the profile request, not a valid ID?
throw new LinkedInException('LinkedIn->invite(): could not send invitation, LinkedIn says: ' . print_r($response['linkedin'], TRUE));
}
break;
}
$data .= ' </invitation-request>
</item-content>
</mailbox-item>';
// send request
$query = self::_URL_API . '/v1/people/~/mailbox';
$response = $this->fetch('POST', $query, $data);
/**
* Check for successful update (a 201 response from LinkedIn server)
* per the documentation linked in method comments above.
*/
return $this->checkResponse(201, $response);
}
/**
* LinkedIn ID validation.
*
* Checks the passed string $id to see if it has a valid LinkedIn ID format,
* which is, as of October 15th, 2010:
*
* 10 alpha-numeric mixed-case characters, plus underscores and dashes.
*
* @param str $id
* A possible LinkedIn ID.
*
* @return bool
* TRUE/FALSE depending on valid ID format determination.
*/
public static function isId($id) {
// check passed data
if(!is_string($id)) {
// bad data passed
throw new LinkedInException('LinkedIn->isId(): bad data passed, $id must be of type string.');
}
$pattern = '/^[a-z0-9_\-]{10}$/i';
if($match = preg_match($pattern, $id)) {
// we have a match
$return_data = TRUE;
} else {
// no match
$return_data = FALSE;
}
return $return_data;
}
/**
* Throttling check.
*
* Checks the passed LinkedIn response to see if we have hit a throttling
* limit:
*
* http://developer.linkedin.com/docs/DOC-1112
*
* @param arr $response
* The LinkedIn response.
*
* @return bool
* TRUE/FALSE depending on content of response.
*/
public static function isThrottled($response) {
$return_data = FALSE;
// check the variable
if(!empty($response) && is_string($response)) {
// we have an array and have a properly formatted LinkedIn response
// store the response in a temp variable
$temp_response = self::xmlToArray($response);
if($temp_response !== FALSE) {
// check to see if we have an error
if(array_key_exists('error', $temp_response) && ($temp_response['error']['children']['status']['content'] == 403) && preg_match('/throttle/i', $temp_response['error']['children']['message']['content'])) {
// we have an error, it is 403 and we have hit a throttle limit
$return_data = TRUE;
}
}
}
return $return_data;
}
/**
* Job posting detail info retrieval function.
*
* The Jobs API returns detailed information about job postings on LinkedIn.
* Find the job summary, description, location, and apply our professional graph