3030class Client
3131{
3232
33+ /**
34+ * Grant type
35+ */
3336 const OAUTH2_GRANT_TYPE = 'authorization_code ' ;
3437
38+ /**
39+ * Response type
40+ */
3541 const OAUTH2_RESPONSE_TYPE = 'code ' ;
3642
3743 /**
@@ -63,11 +69,103 @@ class Client
6369 protected $ redirectUrl ;
6470
6571 /**
72+ * Default authorization URL
6673 * string
6774 */
6875 const OAUTH2_API_ROOT = 'https://www.linkedin.com/oauth/v2/ ' ;
76+
77+ /**
78+ * Default API root URL
79+ * string
80+ */
6981 const API_ROOT = 'https://api.linkedin.com/v1/ ' ;
7082
83+ /**
84+ * API Root URL
85+ *
86+ * @var string
87+ */
88+ protected $ apiRoot = self ::API_ROOT ;
89+
90+ /**
91+ * OAuth API URL
92+ *
93+ * @var string
94+ */
95+ protected $ oAuthApiRoot = self ::OAUTH2_API_ROOT ;
96+
97+ /**
98+ * List of default headers
99+ * @var array
100+ */
101+ protected $ defaultApiHeaders = [
102+ 'Content-Type ' => 'application/json ' ,
103+ 'x-li-format ' => 'json '
104+ ];
105+
106+ /**
107+ * Get list of headers
108+ * @return array
109+ */
110+ public function getDefaultApiHeaders ()
111+ {
112+ return $ this ->defaultApiHeaders ;
113+ }
114+
115+ /**
116+ * Set list of default headers
117+ * @param array $defaultApiHeaders
118+ *
119+ * @return Client
120+ */
121+ public function setDefaultApiHeaders ($ defaultApiHeaders )
122+ {
123+ $ this ->defaultApiHeaders = $ defaultApiHeaders ;
124+ return $ this ;
125+ }
126+
127+ /**
128+ * Obtain API root URL
129+ * @return string
130+ */
131+ public function getApiRoot ()
132+ {
133+ return $ this ->apiRoot ;
134+ }
135+
136+ /**
137+ * Specify API root URL
138+ * @param string $apiRoot
139+ *
140+ * @return Client
141+ */
142+ public function setApiRoot ($ apiRoot )
143+ {
144+ $ this ->apiRoot = $ apiRoot ;
145+ return $ this ;
146+ }
147+
148+ /**
149+ * Get OAuth API root
150+ * @return string
151+ */
152+ public function getOAuthApiRoot ()
153+ {
154+ return $ this ->oAuthApiRoot ;
155+ }
156+
157+ /**
158+ * Set OAuth API root
159+ * @param string $oAuthApiRoot
160+ *
161+ * @return Client
162+ */
163+ public function setOAuthApiRoot ($ oAuthApiRoot )
164+ {
165+ $ this ->oAuthApiRoot = $ oAuthApiRoot ;
166+ return $ this ;
167+ }
168+
71169 /**
72170 * Client constructor.
73171 *
@@ -81,6 +179,7 @@ public function __construct($clientId = '', $clientSecret = '')
81179 }
82180
83181 /**
182+ * Get ClientId
84183 * @return string
85184 */
86185 public function getClientId ()
@@ -89,6 +188,7 @@ public function getClientId()
89188 }
90189
91190 /**
191+ * Set ClientId
92192 * @param string $clientId
93193 *
94194 * @return Client
@@ -100,6 +200,7 @@ public function setClientId($clientId)
100200 }
101201
102202 /**
203+ * Get Client Secret
103204 * @return string
104205 */
105206 public function getClientSecret ()
@@ -108,6 +209,7 @@ public function getClientSecret()
108209 }
109210
110211 /**
212+ * Set Client Secret
111213 * @param string $clientSecret
112214 *
113215 * @return Client
@@ -140,7 +242,7 @@ public function getAccessToken($code = '')
140242 ];
141243 $ uri = $ this ->buildUrl ('accessToken ' , $ params );
142244 $ guzzle = new GuzzleClient ([
143- 'base_uri ' => self :: OAUTH2_API_ROOT ,
245+ 'base_uri ' => $ this -> getOAuthApiRoot () ,
144246 'headers ' => [
145247 'Content-Type ' => 'application/json ' ,
146248 'x-li-format ' => 'json '
@@ -169,7 +271,7 @@ public function getAccessToken($code = '')
169271 }
170272
171273 /**
172- *
274+ * Convert API response into Array
173275 *
174276 * @param \Psr\Http\Message\ResponseInterface $response
175277 *
@@ -203,6 +305,8 @@ public function setAccessToken($accessToken)
203305 }
204306
205307 /**
308+ * Retrieve current active scheme
309+ *
206310 * @return string
207311 */
208312 protected function getCurrentScheme ()
@@ -215,6 +319,7 @@ protected function getCurrentScheme()
215319 }
216320
217321 /**
322+ * Get current URL
218323 * @return string
219324 */
220325 public function getCurrentUrl ()
@@ -225,6 +330,7 @@ public function getCurrentUrl()
225330 }
226331
227332 /**
333+ * Get unique state or specified state
228334 * @return string
229335 */
230336 public function getState ()
@@ -241,7 +347,8 @@ public function getState()
241347 }
242348
243349 /**
244- * @param mixed $state
350+ * Set State
351+ * @param string $state
245352 *
246353 * @return Client
247354 */
@@ -252,6 +359,9 @@ public function setState($state)
252359 }
253360
254361 /**
362+ * Retrieve URL which will be used to send User to LinkedIn
363+ * for authentication
364+ *
255365 * @param array $scope Permissions that your application requires
256366 *
257367 * @return string
@@ -310,9 +420,10 @@ public function setRedirectUrl($redirectUrl)
310420 */
311421 protected function buildUrl ($ endpoint , $ params )
312422 {
313- $ scheme = parse_url (self ::OAUTH2_API_ROOT , PHP_URL_SCHEME );
314- $ authority = parse_url (self ::OAUTH2_API_ROOT , PHP_URL_HOST );
315- $ path = parse_url (self ::OAUTH2_API_ROOT , PHP_URL_PATH );
423+ $ url = $ this ->getOAuthApiRoot ();
424+ $ scheme = parse_url ($ url , PHP_URL_SCHEME );
425+ $ authority = parse_url ($ url , PHP_URL_HOST );
426+ $ path = parse_url ($ url , PHP_URL_PATH );
316427 $ path .= trim ($ endpoint , '/ ' );
317428 $ fragment = '' ;
318429 $ uri = Uri::composeComponents (
@@ -336,13 +447,11 @@ protected function buildUrl($endpoint, $params)
336447 */
337448 public function api ($ endpoint , array $ params = array (), $ method = Method::GET )
338449 {
450+ $ headers = $ this ->getDefaultApiHeaders ();
451+ $ headers ['Authorization ' ] = 'Bearer ' . $ this ->accessToken ->getToken ();
339452 $ guzzle = new GuzzleClient ([
340- 'base_uri ' => self ::API_ROOT ,
341- 'headers ' => [
342- 'Authorization ' => 'Bearer ' . $ this ->accessToken ->getToken (),
343- 'Content-Type ' => 'application/json ' ,
344- 'x-li-format ' => 'json '
345- ]
453+ 'base_uri ' => $ this ->getApiRoot (),
454+ 'headers ' => $ headers
346455 ]);
347456 $ uri = $ endpoint ;
348457 $ options = [];
@@ -382,6 +491,11 @@ public function api($endpoint, array $params = array(), $method = Method::GET)
382491 return self ::responseToArray ($ response );
383492 }
384493
494+ /**
495+ * @param $json
496+ *
497+ * @return null|string
498+ */
385499 private static function extractErrorDescription ($ json )
386500 {
387501 if (isset ($ json ['error_description ' ])) {
0 commit comments