Skip to content

Commit 2e8eeea

Browse files
committed
Updates
1 parent 0af8db4 commit 2e8eeea

File tree

6 files changed

+256
-20
lines changed

6 files changed

+256
-20
lines changed

README.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
PHP LinkedIn Client with OAuth 2 authorization
2-
==============================================
3-
[![Build Status](https://travis-ci.org/zoonman/linkedin-client.svg)](https://travis-ci.org/zoonman/linkedin-client) [![Code Climate](https://codeclimate.com/github/zoonman/linkedin-client/badges/gpa.svg)](https://codeclimate.com/github/zoonman/linkedin-client) [![Packagist](https://img.shields.io/packagist/dt/zoonman/linkedin-client.svg)]() [![GitHub license](https://img.shields.io/github/license/zoonman/linkedin-client.svg)]()
1+
LinkedIn API Client with OAuth 2 authorization witten on PHP
2+
============================================================
3+
[![Build Status](https://travis-ci.org/zoonman/linkedin-client.svg)](https://travis-ci.org/zoonman/linkedin-client) [![Code Climate](https://codeclimate.com/github/zoonman/linkedin-client/badges/gpa.svg)](https://codeclimate.com/github/zoonman/linkedin-client) [![Packagist](https://img.shields.io/packagist/dt/zoonman/linkedin-client.svg)](https://packagist.org/packages/zoonman/linkedin-client) [![GitHub license](https://img.shields.io/github/license/zoonman/linkedin-client.svg)](https://github.com/zoonman/linkedin-client/LICENSE.md)
44

55

66

7-
See [complete example](examples/index.php) to get started.
7+
See [complete example](examples/) inside [index.php](examples/index.php) to get started.
88

99

1010
## Installation
@@ -17,17 +17,22 @@ composer require zoonman/linkedin-client
1717

1818
Or add this package as dependency to `composer.json`.
1919

20+
If you have never used Composer, you should start [here](http://www.phptherightway.com/#composer_and_packagist)
21+
and install composer.
22+
2023

2124
## Usage
2225

2326
To start working with LinkedIn API, you will need to
24-
get client and secret go to
25-
[LinkedIn Developers portal](https://developer.linkedin.com/)
26-
and create new app there.
27+
get application client id and secret.
28+
29+
Go to [LinkedIn Developers portal](https://developer.linkedin.com/)
30+
and create new application in section My Apps.
2731

2832

2933
#### Bootstrapping autoloader and instantiating a client
3034

35+
3136
```php
3237
// ... please, add composer autoloader first
3338
include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
@@ -84,10 +89,50 @@ $accessToken = $client->getAccessToken($_GET['code']);
8489

8590
#### Performing API calls
8691

87-
To perform api call to get profile information
92+
All API calls can be called through simple method:
8893

8994
```php
9095
$profile = $client->api(
96+
'ENDPOINT',
97+
['parameter name' => 'its value here'],
98+
'HTTP method like GET for example'
99+
);
100+
```
101+
102+
To perform api call to get profile information
103+
104+
```php
105+
$profile = $client->get(
91106
'people/~:(id,email-address,first-name,last-name)'
92107
);
108+
print_r($profile);
109+
```
110+
111+
To list companies where you an admin
112+
113+
```php
114+
$profile = $client->get(
115+
'people/~:(id,email-address,first-name,last-name)'
116+
);
117+
print_r($profile);
118+
```
119+
120+
To share content
121+
122+
```php
123+
$share = $client->post(
124+
'people/~/shares',
125+
[
126+
'comment' => 'Checkout this amazing PHP SDK for LinkedIn!',
127+
'content' => [
128+
'title' => 'PHP Client for LinkedIn API',
129+
'description' => 'OAuth 2 flow, composer Package',
130+
'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
131+
'submitted-image-url' => 'https://github.com/fluidicon.png',
132+
],
133+
'visibility' => [
134+
'code' => 'anyone'
135+
]
136+
]
137+
);
93138
```

examples/index.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// import client class
2121
use LinkedIn\Client;
22+
use LinkedIn\Scope;
2223

2324
// import environment variables from the environment file
2425
$dotenv = new Dotenv\Dotenv(dirname(__DIR__));
@@ -51,10 +52,27 @@
5152
pp($accessToken); // print the access token content
5253
echo 'Profile:';
5354
// perform api call to get profile information
54-
$profile = $client->api(
55+
$profile = $client->get(
5556
'people/~:(id,email-address,first-name,last-name)'
5657
);
5758
pp($profile); // print profile information
59+
60+
$share = $client->post(
61+
'people/~/shares',
62+
[
63+
'comment' => 'Checkout this amazing PHP SDK for LinkedIn!',
64+
'content' => [
65+
'title' => 'PHP Client for LinkedIn API',
66+
'description' => 'OAuth 2 flow, composer Package',
67+
'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
68+
'submitted-image-url' => 'https://github.com/fluidicon.png',
69+
],
70+
'visibility' => [
71+
'code' => 'anyone'
72+
]
73+
]
74+
);
75+
pp($share);
5876
} catch (\LinkedIn\Exception $exception) {
5977
// in case of failure, provide with details
6078
pp($exception);
@@ -78,13 +96,8 @@
7896
echo '<a href="/">Start over</a>';
7997
} else {
8098
// define desired list of scopes
81-
$scopes = [
82-
'r_basicprofile',
83-
'r_emailaddress',
84-
'rw_company_admin',
85-
'w_share',
86-
];
87-
$loginUrl = $client->getLoginUrl(); // get url on LinkedIn to start linking
99+
$scopes = Scope::getValues();
100+
$loginUrl = $client->getLoginUrl($scopes); // get url on LinkedIn to start linking
88101
$_SESSION['state'] = $client->getState(); // save state for future validation
89102
$_SESSION['redirect_url'] = $client->getRedirectUrl(); // save redirect url for future validation
90103
echo 'LoginUrl: <a href="'.$loginUrl.'">' . $loginUrl. '</a>';

src/AbstractEnum.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* linkedin-client
4+
* AbstractEnum.php
5+
*
6+
* PHP Version 5
7+
*
8+
* @category Production
9+
* @package Default
10+
* @author Philipp Tkachev <zoonman@gmail.com>
11+
* @date 8/22/17 09:10
12+
* @license http://linkedin-client.com/license.txt linkedin-client License
13+
* @version GIT: 1.0
14+
* @link http://linkedin-client.com/
15+
*/
16+
17+
namespace LinkedIn;
18+
19+
/**
20+
* Class AbstractEnum
21+
*
22+
* @package LinkedIn
23+
*/
24+
abstract class AbstractEnum
25+
{
26+
27+
/**
28+
* @return array
29+
*/
30+
public static function getMap()
31+
{
32+
$spl = new \ReflectionClass(get_called_class());
33+
return $spl->getConstants();
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public static function getValues()
40+
{
41+
return array_values(static::getMap());
42+
}
43+
}

src/Client.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use GuzzleHttp\Exception\RequestException;
2121
use function GuzzleHttp\Psr7\build_query;
2222
use GuzzleHttp\Psr7\Uri;
23+
use LinkedIn\Http\Method;
2324

2425
/**
2526
* Class Client
@@ -312,14 +313,15 @@ protected function buildUrl($endpoint, $params)
312313
}
313314

314315
/**
316+
* Perform API call to LinkedIn
315317
* @param string $endpoint
316318
* @param array $params
317319
* @param string $method
318320
*
319321
* @return array
320322
* @throws \LinkedIn\Exception
321323
*/
322-
public function api($endpoint, array $params = array(), $method = 'GET')
324+
public function api($endpoint, array $params = array(), $method = Method::GET)
323325
{
324326
$guzzle = new GuzzleClient([
325327
'base_uri' => self::API_ROOT,
@@ -330,13 +332,28 @@ public function api($endpoint, array $params = array(), $method = 'GET')
330332
]
331333
]);
332334
$uri = $endpoint;
335+
$options = [];
333336
//$params['oauth2_access_token'] = $this->accessToken->getToken();
334-
if (!empty($params)) {
335-
$uri .= '?' . build_query($params);
337+
switch ($method) {
338+
case Method::GET:
339+
if (!empty($params)) {
340+
$uri .= '?' . build_query($params);
341+
}
342+
break;
343+
case Method::POST:
344+
$options['body'] = \GuzzleHttp\json_encode($params);
345+
break;
346+
default:
347+
throw new Exception(
348+
"Method not defined",
349+
1,
350+
null,
351+
"Please, pass correct method!"
352+
);
336353
}
337354

338355
try {
339-
$response = $guzzle->request($method, $uri);
356+
$response = $guzzle->request($method, $uri, $options);
340357
} catch (RequestException $requestException) {
341358
$json = self::responseToArray(
342359
$requestException->getResponse()
@@ -351,4 +368,28 @@ public function api($endpoint, array $params = array(), $method = 'GET')
351368
}
352369
return self::responseToArray($response);
353370
}
371+
372+
/**
373+
* Make API call to LinkedIn using GET method
374+
* @param string $endpoint
375+
* @param array $params
376+
*
377+
* @return array
378+
*/
379+
public function get($endpoint, array $params = array())
380+
{
381+
return $this->api($endpoint, $params, $method = Method::GET);
382+
}
383+
384+
/**
385+
* Make API call to LinkedIn using POST method
386+
* @param string $endpoint
387+
* @param array $params
388+
*
389+
* @return array
390+
*/
391+
public function post($endpoint, array $params = array())
392+
{
393+
return $this->api($endpoint, $params, $method = Method::POST);
394+
}
354395
}

src/Http/Method.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* linkedin-client
4+
* Method.php
5+
*
6+
* PHP Version 5
7+
*
8+
* @category Production
9+
* @package Default
10+
* @author Philipp Tkachev <zoonman@gmail.com>
11+
* @date 8/22/17 09:15
12+
* @license http://linkedin-client.com/license.txt linkedin-client License
13+
* @version GIT: 1.0
14+
* @link http://linkedin-client.com/
15+
*/
16+
17+
namespace LinkedIn\Http;
18+
19+
use LinkedIn\AbstractEnum;
20+
21+
class Method extends AbstractEnum
22+
{
23+
24+
/**
25+
*
26+
*/
27+
const CONNECT = 'CONNECT';
28+
29+
/**
30+
* The GET method requests a representation of the specified resource.
31+
* Requests using GET should only retrieve data.
32+
*/
33+
const GET = 'GET';
34+
35+
/**
36+
*
37+
*/
38+
const HEAD = 'HEAD';
39+
40+
/**
41+
*
42+
*/
43+
const POST = 'POST';
44+
45+
/**
46+
*
47+
*/
48+
const PUT = 'PUT';
49+
50+
/**
51+
*
52+
*/
53+
const PATCH = 'PATCH';
54+
55+
/**
56+
*
57+
*/
58+
const OPTIONS = 'OPTIONS';
59+
60+
/**
61+
*
62+
*/
63+
const DELETE = 'DELETE';
64+
65+
/**
66+
*
67+
*/
68+
const TRACE = 'TRACE';
69+
}

src/Scope.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* linkedin-client
4+
* Scope.php
5+
*
6+
* PHP Version 5
7+
*
8+
* @category Production
9+
* @package Default
10+
* @author Philipp Tkachev <zoonman@gmail.com>
11+
* @date 8/22/17 09:02
12+
* @license http://linkedin-client.com/license.txt linkedin-client License
13+
* @version GIT: 1.0
14+
* @link http://linkedin-client.com/
15+
*/
16+
17+
namespace LinkedIn;
18+
19+
class Scope extends AbstractEnum
20+
{
21+
const READ_BASIC_PROFILE = 'r_basicprofile';
22+
const READ_EMAIL_ADDRESS = 'r_emailaddress';
23+
const MANAGE_COMPANY = 'rw_company_admin';
24+
const SHARING = 'w_share';
25+
}

0 commit comments

Comments
 (0)