Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => [
'elements' => [
'arguments',
'arrays',
'match',
]
Expand Down
104 changes: 45 additions & 59 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
[![Latest Stable Version](https://poser.pugx.org/elninotech/linkedin-api-client/v)](https://packagist.org/packages/elninotech/linkedin-api-client)
[![Total Downloads](https://poser.pugx.org/elninotech/linkedin-api-client/downloads)](https://packagist.org/packages/elninotech/linkedin-api-client/stats)


A PHP library to handle authentication and communication with LinkedIn API. The library/SDK helps you to get an access
token and when authenticated it helps you to send API requests. You will not get *everything* for free though... You
have to read the [LinkedIn documentation][api-doc-core] to understand how you should query the API.
have to read the [LinkedIn documentation][api-doc-core] to understand how you should query the API.

To get an overview what this library actually is doing for you. Take a look at the authentication page from
the [API docs][api-doc-authentication].
Expand All @@ -19,55 +18,27 @@ Here is a list of features that might convince you to choose this LinkedIn clien

* Flexible and easy to extend
* Developed with modern PHP standards
* Not developed for a specific framework.
* Not developed for a specific framework.
* Handles the authentication process
* Respects the CSRF protection

## Installation

**TL;DR**
```bash
composer require php-http/curl-client guzzlehttp/psr7 php-http/message elninotech/linkedin-api-client
```

This library does not have a dependency on Guzzle or any other library that sends HTTP requests. We use the awesome
HTTPlug to achieve the decoupling. We want you to choose what library to use for sending HTTP requests. Consult this list
of packages that support [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation)
find clients to use. For more information about virtual packages please refer to
[HTTPlug](http://docs.php-http.org/en/latest/httplug/users.html). Example:

```bash
composer require php-http/guzzle7-adapter
```

You do also need to install a PSR-7 implementation and a factory to create PSR-7 messages (PSR-17 whenever that is
released). You could use Guzzles PSR-7 implementation and factories from php-http:
First, install Linkedin-API-client via the [Composer][composer] package manager:

```bash
composer require guzzlehttp/psr7 php-http/message
composer require elninotech/linkedin-api-client
```

Now you may install the library by running the following:
Ensure that the `php-http/discovery` composer plugin is allowed to run or install a client manually if your project does
not already have a PSR-18 client integrated.

```bash
composer require elninotech/linkedin-api-client
composer require guzzlehttp/guzzle
```

If you are updating from a previous version, make sure to read [the upgrade documentation](Upgrade.md).

### Finding the HTTP client (optional)

The LinkedIn client needs to know what library you are using to send HTTP messages. You could provide an instance of
HttpClient and MessageFactory or you could fall back to auto discovery. Below is an example of where you provide a Guzzle7
instance.

```php
$linkedIn=new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn->setHttpClient(new \Http\Adapter\Guzzle7\Client());
$linkedIn->setHttpMessageFactory(new Http\Message\MessageFactory\GuzzleMessageFactory());

```

## Usage

In order to use this API client (or any other LinkedIn clients), you have to [register your application][register-app]
Expand All @@ -91,7 +62,7 @@ This example below is showing how to login with LinkedIn.
*/
//require_once "vendor/autoload.php";

$linkedIn=new Elnino\LinkedIn\LinkedIn('client_id', 'client_secret');
$linkedIn = new Elnino\LinkedIn\LinkedIn('client_id', 'client_secret');

if ($linkedIn->isAuthenticated()) {
//we know that the user is authenticated now. Start query the API
Expand All @@ -112,10 +83,10 @@ echo "<a href='$url'>Login with LinkedIn</a>";

### How to post on LinkedIn wall

The example below shows how you can post on a users wall. The access token is fetched from the database.
The example below shows how you can post on a users wall. The access token is fetched from the database.

```php
$linkedIn=new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn = new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn->setAccessToken('access_token_from_db');

$options = array('json'=>
Expand All @@ -142,15 +113,15 @@ var_dump($result);

### The api options

The third parameter of `LinkedIn::api` is an array with options. Below is a table of array keys that you may use.
The third parameter of `LinkedIn::api` is an array with options. Below is a table of array keys that you may use.

| Option name | Description
| ----------- | -----------
| body | The body of a HTTP request. Put your json string here.
| headers | This is HTTP headers to the request
| json | This is an array with json data that will be encoded to a json string.
| response_data_type | To override the response format for one request
| query | This is an array with query parameters
| Option name | Description
|--------------------|------------------------------------------------------------------------
| body | The body of a HTTP request. Put your json string here.
| headers | This is HTTP headers to the request
| json | This is an array with json data that will be encoded to a json string.
| response_data_type | To override the response format for one request
| query | This is an array with query parameters

### Understanding response data type

Expand All @@ -159,7 +130,7 @@ The data type returned from `LinkedIn::api` can be configured. You may use the t

```php
// By constructor argument
$linkedIn=new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret', 'array');
$linkedIn = new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret', 'array');

// By setter
$linkedIn->setResponseDataType('string');
Expand All @@ -171,29 +142,29 @@ $linkedIn->get('/v2/me/?projection=(id,firstName,lastName)', array('response_dat

Below is a table that specifies what the possible return data types are when you call `LinkedIn::api`.

| Type | Description
| ------ | ------------
| array | An assosiative array. This can only be used with the `json` format.
| psr7 | A PSR7 response.
| stream | A file stream.
| string | A plain old string.

| Type | Description
|--------|---------------------------------------------------------------------
| array | An assosiative array. This can only be used with the `json` format.
| psr7 | A PSR7 response.
| stream | A file stream.
| string | A plain old string.

### Use different Session classes

You might want to use another storage than the default `SessionStorage`. If you are using Laravel
you are more likely to inject the `IlluminateSessionStorage`.
you are more likely to inject the `IlluminateSessionStorage`.

```php
$linkedIn=new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn = new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn->setStorage(new IlluminateSessionStorage());
```

You can inject any class implementing `DataStorageInterface`. You can also inject different `UrlGenerator` classes.

### Using different scopes

If you want to define special scopes when you authenticate the user you should specify them when you are generating the
login url. If you don't specify scopes LinkedIn will use the default scopes that you have configured for the app.
If you want to define special scopes when you authenticate the user you should specify them when you are generating the
login url. If you don't specify scopes LinkedIn will use the default scopes that you have configured for the app.

```php
$scope = 'r_fullprofile,r_emailaddress,w_share';
Expand All @@ -204,11 +175,26 @@ $url = $linkedIn->getLoginUrl(array('scope'=>$scope));
echo "<a href='$url'>Login with LinkedIn</a>";
```

### Using a different http client

If you want to customize how the requests are being sent, you can inject a different `RequestManager` when instantiating
the API.

```php
$myRequestManager = new MySpecialRequestManager();
$linkedIn = new Elnino\LinkedIn\LinkedIn('app_id', 'app_secret', 'array', $myRequestManager);
```

### Special thanks

This repo was originally created by [Thomas Nyholm from Happyr][forked-from].

[composer]: https://getcomposer.org/

[register-app]: https://www.linkedin.com/developers/apps

[api-doc-authentication]: https://learn.microsoft.com/en-us/linkedin/shared/authentication/authentication

[api-doc-core]: https://learn.microsoft.com/en-us/linkedin/shared/api-guide/concepts

[forked-from]: https://github.com/Happyr/LinkedIn-API-client
12 changes: 10 additions & 2 deletions Upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

This document explains how you upgrade from one version to another.

## Upgrade from 1.0 to 2.0
## Upcoming

### Changes

* Using PSR 17 Http Client and PSR 18 Factory
* Ability to inject RequestManager when creating a new client.
* Deprecate specifying protocolVersion

## Upgrade from 1.0 to 2.0.0

### Changes

Expand All @@ -18,7 +26,7 @@ This document explains how you upgrade from one version to another.

### Changes

* We do not longer require `php-http/message`. You have to make sure to put that in your own composer.json.
* We do no longer require `php-http/message`. You have to make sure to put that in your own composer.json.

## Upgrade from 0.7.1 to 0.7.2

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
],
"require": {
"php": "^7.2 || ^8.0",
"ext-json": "*",
"php-http/discovery": "^1.0",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message-factory": "^1.0",
"psr/http-client-implementation": "^1.0"
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0"
},
"require-dev": {
"ext-json": "*",
"guzzlehttp/guzzle": "^7.0",
"illuminate/support": "^12.0",
"mockery/mockery": "^1.0",
"php-http/guzzle7-adapter": "^1.0"
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -45,13 +44,14 @@
},
"config": {
"allow-plugins": {
"php-http/discovery": true
"php-http/discovery": false
}
},
"scripts": {
"lint": "./tools/php-cs-fixer check",
"lint-fix": "./tools/php-cs-fixer fix",
"phpstan": "./tools/phpstan analyse --memory-limit=-1",
"test": "./tools/phpunit"
"test": "./tools/phpunit",
"test-coverage": "XDEBUG_MODE=coverage ./tools/phpunit --coverage-text"
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
level: 6
treatPhpDocTypesAsCertain: false
paths:
- src
2 changes: 1 addition & 1 deletion src/AuthenticatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function fetchNewAccessToken(LinkedInUrlGeneratorInterface $urlGenerator)
/**
* Generate a login url.
*
* @param array<mixed> $options
* @param mixed[] $options
*
* @return string
*/
Expand Down
Loading