1 | <?php |
||
32 | class LinkedIn |
||
33 | { |
||
34 | /** |
||
35 | * The OAuth access token received in exchange for a valid authorization |
||
36 | * code. null means the access token has yet to be determined. |
||
37 | * |
||
38 | * @var AccessToken |
||
39 | */ |
||
40 | protected $accessToken = null; |
||
41 | |||
42 | /** |
||
43 | * @var string format |
||
44 | */ |
||
45 | private $format; |
||
46 | |||
47 | /** |
||
48 | * @var string responseFormat |
||
49 | */ |
||
50 | private $responseDataType; |
||
51 | |||
52 | /** |
||
53 | * @var ResponseInterface |
||
54 | */ |
||
55 | private $lastResponse; |
||
56 | |||
57 | /** |
||
58 | * @var RequestManager |
||
59 | */ |
||
60 | private $requestManager; |
||
61 | |||
62 | /** |
||
63 | * @var Authenticator |
||
64 | */ |
||
65 | private $authenticator; |
||
66 | |||
67 | /** |
||
68 | * @var UrlGeneratorInterface |
||
69 | */ |
||
70 | private $urlGenerator; |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | * |
||
75 | * @param string $appId |
||
76 | * @param string $appSecret |
||
77 | * @param string $format 'json', 'xml' |
||
78 | * @param string $responseDataType 'array', 'string', 'simple_xml' 'psr7', 'stream' |
||
79 | */ |
||
80 | public function __construct($appId, $appSecret, $format = 'json', $responseDataType = 'array') |
||
88 | |||
89 | /** |
||
90 | * Is the current user authenticated? |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function isAuthenticated() |
||
105 | |||
106 | /** |
||
107 | * Make an API call. Read about what calls that are possible here: https://developer.linkedin.com/docs/rest-api. |
||
108 | * |
||
109 | * Example: |
||
110 | * $linkedIn->api('GET', '/v1/people/~:(id,firstName,lastName,headline)'); |
||
111 | * |
||
112 | * The options: |
||
113 | * - body: the body of the request |
||
114 | * - format: the format you are using to send the request |
||
115 | * - headers: array with headers to use |
||
116 | * - response_data_type: the data type to get back |
||
117 | * - query: query parameters to the request |
||
118 | * |
||
119 | * @param string $method This is the HTTP verb |
||
120 | * @param string $resource everything after the domain in the URL. |
||
121 | * @param array $options See the readme for option description. |
||
122 | * |
||
123 | * @return mixed this depends on the response_data_type parameter. |
||
124 | */ |
||
125 | public function api($method, $resource, array $options = array()) |
||
126 | { |
||
127 | // Add access token to the headers |
||
128 | $options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken()); |
||
129 | |||
130 | // Do logic and adjustments to the options |
||
131 | $requestFormat = $this->filterRequestOption($options); |
||
132 | |||
133 | // Generate an url |
||
134 | $url = $this->getUrlGenerator()->getUrl( |
||
135 | 'api', |
||
136 | $resource, |
||
137 | isset($options['query']) ? $options['query'] : array() |
||
138 | ); |
||
139 | |||
140 | $body = isset($options['body']) ? $options['body'] : null; |
||
141 | $this->lastResponse = $this->getRequestManager()->sendRequest($method, $url, $options['headers'], $body); |
||
142 | |||
143 | //Get the response data format |
||
144 | if (isset($options['response_data_type'])) { |
||
145 | $responseDataType = $options['response_data_type']; |
||
146 | } else { |
||
147 | $responseDataType = $this->getResponseDataType(); |
||
148 | } |
||
149 | |||
150 | return ResponseConverter::convert($this->lastResponse, $requestFormat, $responseDataType); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Modify and filter the request options. Make sure we use the correct query parameters and headers. |
||
155 | * |
||
156 | * @param array &$options |
||
157 | * |
||
158 | * @return string the request format to use |
||
159 | */ |
||
160 | protected function filterRequestOption(array &$options) |
||
161 | { |
||
162 | if (isset($options['json'])) { |
||
163 | $options['format'] = 'json'; |
||
164 | $options['body'] = json_encode($options['json']); |
||
165 | } elseif (!isset($options['format'])) { |
||
166 | // Make sure we always have a format |
||
167 | $options['format'] = $this->getFormat(); |
||
168 | } |
||
169 | |||
170 | // Set correct headers for this format |
||
171 | switch ($options['format']) { |
||
172 | case 'xml': |
||
173 | $options['headers']['Content-Type'] = 'text/xml'; |
||
174 | break; |
||
175 | case 'json': |
||
176 | $options['headers']['Content-Type'] = 'application/json'; |
||
177 | $options['headers']['x-li-format'] = 'json'; |
||
178 | $options['query']['format'] = 'json'; |
||
179 | break; |
||
180 | default: |
||
181 | // Do nothing |
||
182 | } |
||
183 | |||
184 | return $options['format']; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get a login URL where the user can put his/hers LinkedIn credentials and authorize the application. |
||
189 | * |
||
190 | * The options: |
||
191 | * - redirect_uri: the url to go to after a successful login |
||
192 | * - scope: comma (or space) separated list of requested extended permissions |
||
193 | * |
||
194 | * @param array $options Provide custom parameters |
||
195 | * |
||
196 | * @return string The URL for the login flow |
||
197 | */ |
||
198 | public function getLoginUrl($options = array()) |
||
209 | |||
210 | /** |
||
211 | * See docs for LinkedIn::api(). |
||
212 | * |
||
213 | * @param string $resource |
||
214 | * @param array $options |
||
215 | * |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function get($resource, array $options = array()) |
||
219 | { |
||
220 | return $this->api('GET', $resource, $options); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * See docs for LinkedIn::api(). |
||
225 | * |
||
226 | * @param string $resource |
||
227 | * @param array $options |
||
228 | * |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public function post($resource, array $options = array()) |
||
235 | |||
236 | /** |
||
237 | * Clear the data storage. This will forget everything about the user and authentication process. |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function clearStorage() |
||
247 | |||
248 | /** |
||
249 | * If the user has canceled the login we will return with an error. |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function hasError() |
||
254 | { |
||
255 | return GlobalVariableGetter::has('error'); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Returns a LoginError or null. |
||
260 | * |
||
261 | * @return LoginError|null |
||
262 | */ |
||
263 | public function getError() |
||
269 | |||
270 | /** |
||
271 | * Get the default format to use when sending requests. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getFormat() |
||
279 | |||
280 | /** |
||
281 | * Set the default format to use when sending requests. |
||
282 | * |
||
283 | * @param string $format |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function setFormat($format) |
||
288 | { |
||
289 | $this->format = $format; |
||
290 | |||
291 | return $this; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Get the default data type to be returned as a response. |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getResponseDataType() |
||
303 | |||
304 | /** |
||
305 | * Set the default data type to be returned as a response. |
||
306 | * |
||
307 | * @param string $responseDataType |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function setResponseDataType($responseDataType) |
||
317 | |||
318 | /** |
||
319 | * Get the last response. This will always return a PSR-7 response no matter of the data type used. |
||
320 | * |
||
321 | * @return ResponseInterface|null |
||
322 | */ |
||
323 | public function getLastResponse() |
||
327 | |||
328 | /** |
||
329 | * Returns an access token. If we do not have one in memory, try to fetch one from a *code* in the $_REQUEST. |
||
330 | * |
||
331 | * @return AccessToken|null The access token of null if the access token is not found |
||
332 | */ |
||
333 | public function getAccessToken() |
||
344 | |||
345 | /** |
||
346 | * If you have stored a previous access token in a storage (database) you could set it here. After setting an |
||
347 | * access token you have to make sure to verify it is still valid by running LinkedIn::isAuthenticated. |
||
348 | * |
||
349 | * @param string|AccessToken $accessToken |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function setAccessToken($accessToken) |
||
363 | |||
364 | /** |
||
365 | * Set a URL generator. |
||
366 | * |
||
367 | * @param UrlGeneratorInterface $urlGenerator |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setUrlGenerator(UrlGeneratorInterface $urlGenerator) |
||
377 | |||
378 | /** |
||
379 | * @return UrlGeneratorInterface |
||
380 | */ |
||
381 | protected function getUrlGenerator() |
||
389 | |||
390 | /** |
||
391 | * Set a data storage. |
||
392 | * |
||
393 | * @param DataStorageInterface $storage |
||
394 | * |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function setStorage(DataStorageInterface $storage) |
||
403 | |||
404 | /** |
||
405 | * Set a http client. |
||
406 | * |
||
407 | * @param HttpClient $client |
||
408 | * |
||
409 | * @return $this |
||
410 | * |
||
411 | * @deprecated This function will be removed in 0.7.0. It will be replaced by setHttpAdapter |
||
412 | */ |
||
413 | public function setHttpClient(HttpClient $client) |
||
419 | |||
420 | /** |
||
421 | * @return RequestManager |
||
422 | */ |
||
423 | protected function getRequestManager() |
||
427 | |||
428 | /** |
||
429 | * @return Authenticator |
||
430 | */ |
||
431 | protected function getAuthenticator() |
||
435 | } |
||
436 |