Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return headers if $response->Body() is null #35

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
### Attention!

This a fork from the original code from [Zoonman](https://github.com/zoonman/linkedin-api-php-client), so almost all the credit for this library goes for him.

I just made a single change in order to adapt the code for avoid an error pusblishing a new post, because Linkedin has migrated the api to the V2 Version.

I still have to addapt some other changes (like adding new scopes for the V2 version), but I thought it would be better to publish a new package, so it can be used with composer without facing the commented error.

Most of the examples from the original Readme are deprecated, and they should be fixed too.


LinkedIn API Client with OAuth 2 authorization written on PHP
============================================================
[![Build Status](https://travis-ci.org/zoonman/linkedin-api-php-client.svg?branch=master)](https://travis-ci.org/zoonman/linkedin-api-php-client) [![Code Climate](https://codeclimate.com/github/zoonman/linkedin-api-php-client/badges/gpa.svg)](https://codeclimate.com/github/zoonman/linkedin-api-php-client) [![Packagist](https://img.shields.io/packagist/dt/zoonman/linkedin-api-php-client.svg)](https://packagist.org/packages/zoonman/linkedin-api-php-client) [![GitHub license](https://img.shields.io/github/license/zoonman/linkedin-api-php-client.svg)](https://github.com/zoonman/linkedin-api-php-client/blob/master/LICENSE.md)
Expand Down Expand Up @@ -31,6 +42,7 @@ get application client id and secret.
Go to [LinkedIn Developers portal](https://developer.linkedin.com/)
and create new application in section My Apps.

All the documentation about using the api now resides in this [documentation](https://docs.microsoft.com/en-us/linkedin/marketing/index)

#### Bootstrapping autoloader and instantiating a client

Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "zoonman/linkedin-api-php-client",
"name": "samoritano/linkedin-api-php-client-v2",
"description": "LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.",
"type": "library",
"keywords": [
Expand All @@ -9,7 +9,7 @@
"social", "rest", "api", "client", "social network", "auth", "authorization",
"wrapper", "integration", "platform"
],
"homepage": "https://github.com/zoonman/linkedin-api-php-client",
"homepage": "https://github.com/samoritano/linkedin-api-php-client-v2",
"require": {
"php": ">=5.6",
"ext-curl": "*",
Expand All @@ -33,13 +33,16 @@
"name": "Daniel J. Post",
"homepage": "http://danieljpost.info/",
"role": "Developer"
},
{
"name": "Samori Bokoko",
"role": "Developer"
}
],
"support": {
"docs": "https://www.zoonman.com/projects/linkedin-client/",
"source": "https://github.com/zoonman/linkedin-api-php-client",
"issues": "https://github.com/zoonman/linkedin-api-php-client/issues",
"wiki": "https://github.com/zoonman/linkedin-api-php-client/wiki"
"source": "https://github.com/samoritano/linkedin-api-php-client-v2",
"issues": "https://github.com/samoritano/linkedin-api-php-client-v2/issues",
"wiki": "https://github.com/samoritano/linkedin-api-php-client-v2/wiki"
},
"autoload": {
"psr-4": {"LinkedIn\\": "src/"}
Expand Down
97 changes: 84 additions & 13 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,34 @@ class AccessToken implements \JsonSerializable
protected $expiresAt;

/**
* AccessToken constructor.
* Get token string
*
* @param string $token
* @param int $expiresAt
* @return string
*/
public function __construct($token = '', $expiresAt = 0)
{
$this->setToken($token);
$this->setExpiresAt($expiresAt);
}

/**
* Get token string
*
* @return string
* @var string
*/

protected $refreshToken;

/**
* @var int
*/

protected $refreshTokenExpiresAt;


public function getToken()
{
return $this->token;
}

public function getRefreshToken()
{
return $this->refreshToken;
}

/**
* Set token string
*
Expand All @@ -79,6 +86,19 @@ public function setToken($token)
return $this;
}

/**
* Set refresh token string
*
* @param string $refreshToken
*
* @return AccessToken
*/
public function setRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;
return $this;
}

/**
* The number of seconds remaining, from the time it was requested, before the token will expire.
*
Expand All @@ -89,6 +109,21 @@ public function getExpiresIn()
return $this->expiresAt - time();
}

/**
* AccessToken constructor.
*
* @param string $token
* @param string $token
* @param int $expiresAt
*/
public function __construct($token = '', $expiresAt = 0,$refreshToken = '', $refreshTokenExpiresAt = 0 )
{
$this->setToken($token);
$this->setExpiresAt($expiresAt);
$this->setRefreshToken($refreshToken);
$this->setRefreshTokenExpiresAt($refreshTokenExpiresAt);
}

/**
* Set token expiration time
*
Expand Down Expand Up @@ -122,6 +157,16 @@ public function getExpiresAt()
return $this->expiresAt;
}

/**
* Get Unix epoch time when refresh token will expire
*
* @return int
*/
public function getRefreshTokenExpiresAt()
{
return $this->refreshTokenExpiresAt;
}

/**
* Set Unix epoch time when token will expire
*
Expand All @@ -132,9 +177,21 @@ public function getExpiresAt()
public function setExpiresAt($expiresAt)
{
$this->expiresAt = $expiresAt;
return $this;
}

/**
* Set Unix epoch time when token will expire
*
* @param int $refreshTokenExpiresAt seconds, unix time
*

*/
public function setRefreshTokenExpiresAt($refreshTokenExpiresAt)
{
$this->refreshTokenExpiresAt = $refreshTokenExpiresAt;
}


/**
* Convert API response into AccessToken
*
Expand Down Expand Up @@ -173,9 +230,21 @@ public static function fromResponseArray($responseArray)
'Access token expiration date is not specified'
);
}
if (!isset($responseArray['refresh_token'])) {
throw new \InvalidArgumentException(
'Refresh token is not available'
);
}
if (!isset($responseArray['refresh_token_expires_in'])) {
throw new \InvalidArgumentException(
'Refresh token expiration date is not specified'
);
}
return new static(
$responseArray['access_token'],
$responseArray['expires_in'] + time()
$responseArray['expires_in'] + time(),
$responseArray['refresh_token'],
$responseArray['refresh_token_expires_in'] + time()
);
}

Expand All @@ -187,6 +256,8 @@ public function jsonSerialize()
return [
'token' => $this->getToken(),
'expiresAt' => $this->getExpiresAt(),
'refreshToken' => $this->getRefreshToken(),
'refreshTokenExpiresAt' => $this->getRefreshTokenExpiresAt()
];
}
}
16 changes: 12 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,18 @@ public function getAccessToken($code = '')
*/
public static function responseToArray($response)
{
return \GuzzleHttp\json_decode(
$response->getBody()->getContents(),
true
);
if ($contents = $response->getBody()->getContents()) {
return \GuzzleHttp\json_decode(
$contents,
true
);
}

if ($contents = $response->getHeaders()) {
return $contents;
}

return [];
}

/**
Expand Down