-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedin.php
More file actions
110 lines (87 loc) · 2.46 KB
/
linkedin.php
File metadata and controls
110 lines (87 loc) · 2.46 KB
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
<?php
class LinkedInException extends Exception
{
}
class LinkedIn
{
protected $_apiKey;
protected $_apiSecret;
protected $_accessToken = null;
protected $_callbackUrl;
const API_BASE_URI = 'https://api.linkedin.com/v1';
const OAUTH_BASE_URI = 'https://www.linkedin.com/uas/oauth2';
public function __construct($config)
{
$this->_apiKey = $config['apiKey'];
$this->_apiSecret = $config['apiSecret'];
$this->_callbackUrl = $config['callbackUrl'];
}
public function getLoginUrl($scope="")
{
$params = array('response_type' => 'code',
'client_id' => $this->_apiKey,
'scope' => $scope,
'state' => uniqid('', true),
'redirect_uri' => $this->_callbackUrl,
);
$uri = self::OAUTH_BASE_URI.'/authorization?' . http_build_query($params);
$_SESSION['state'] = $params['state'];
return $uri;
}
public function getAccessToken($code)
{
if ($this->_accessToken !== null)
{
return $this->_accessToken;
}
$params = array('grant_type' => 'authorization_code',
'client_id' => $this->_apiKey,
'client_secret' => $this->_apiSecret,
'code' => $code,
'redirect_uri' => $this->_callbackUrl,
);
$uri = self::OAUTH_BASE_URI . '/accessToken?' . http_build_query($params);
$context = stream_context_create(
array(
'http' => array(
'method' => 'POST'
)
)
);
$response = file_get_contents($uri, false, $context);
$token = json_decode($response);
$this->_accessToken = $token->access_token;
$_SESSION['expires_in'] = $token->expires_in;
$_SESSION['expires_at'] = time() + $_SESSION['expires_in'];
return $this->_accessToken;
}
public function setAccessToken($token)
{
$this->_accessToken = $token;
}
public function makeRequest($method, $request, $params=array(), $format = 'json')
{
$oauth_params = array(
'oauth2_access_token' => $this->_accessToken,
'format' => $format,
);
if(is_array($params))
$params = http_build_query($params);
$uri = self::API_BASE_URI . $request . '?' . http_build_query($oauth_params) . '&' . $params;
$context = stream_context_create(
array(
'http' => array(
'method' => $method,
)
)
);
$response = file_get_contents($uri, false, $context);
return json_decode($response);
}
public function get($endpoint, $options){
return $this->makeRequest('GET', $endpoint, $options);
}
public function post($endpoint, $options){
return $this->makeRequest('POST', $endpoint, $options);
}
}