Skip to content

Commit ef8e244

Browse files
committed
Add ability to override API root and headers list
1 parent a681806 commit ef8e244

File tree

2 files changed

+170
-15
lines changed

2 files changed

+170
-15
lines changed

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ $client->get('ENDPOINT', ['param' => 'value']);
150150
$client->post('ENDPOINT', ['param' => 'value']);
151151
```
152152

153+
#### Examples
153154

154-
To perform api call to get profile information
155+
Perform api call to get profile information
155156

156157
```php
157158
$profile = $client->get(
@@ -160,7 +161,7 @@ $profile = $client->get(
160161
print_r($profile);
161162
```
162163

163-
To list companies where you are an admin
164+
List companies where you are an admin
164165

165166
```php
166167
$profile = $client->get(
@@ -170,7 +171,7 @@ $profile = $client->get(
170171
print_r($profile);
171172
```
172173

173-
To share content on a personal profile
174+
Share content on a personal profile
174175

175176
```php
176177
$share = $client->post(
@@ -190,6 +191,46 @@ $share = $client->post(
190191
);
191192
```
192193

194+
Share content on a LinkedIn business page
195+
196+
```php
197+
// set sandboxed company page to work with
198+
// you can check updates at
199+
// https://www.linkedin.com/company/devtestco
200+
$companyId = '2414183';
201+
202+
$share = $client->post(
203+
'companies/' . $companyId . '/shares',
204+
[
205+
'comment' => 'Checkout this amazing PHP SDK for LinkedIn!',
206+
'content' => [
207+
'title' => 'PHP Client for LinkedIn API',
208+
'description' => 'OAuth 2 flow, composer Package',
209+
'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
210+
'submitted-image-url' => 'https://github.com/fluidicon.png',
211+
],
212+
'visibility' => [
213+
'code' => 'anyone'
214+
]
215+
]
216+
);
217+
```
218+
219+
Setup custom API request headers
220+
221+
```php
222+
$client->setDefaultApiHeaders([
223+
'Content-Type' => 'application/json',
224+
'x-li-format' => 'json',
225+
'x-li-src' => 'msdk' // set a src header to "msdk" to mimic a mobile SDK
226+
]);
227+
```
228+
229+
Change default API root
230+
231+
```php
232+
$client->setApiRoot('https://api.linkedin.com/v2/');
233+
```
193234

194235
## Contributing
195236

src/Client.php

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
class 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

Comments
 (0)