-
Notifications
You must be signed in to change notification settings - Fork 116
/
index.php
192 lines (176 loc) · 6.86 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* linkedin-client
* index.php
*
* PHP Version 5
*
* @category Production
* @package Default
* @author Philipp Tkachev <philipp@zoonman.com>
* @date 8/17/17 22:47
* @license http://zoonman.com/license.txt linkedin-client License
* @version GIT: 1.0
* @link http://zoonman.com/projects/linkedin-client
*/
// add Composer autoloader
include_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
// import client class
use LinkedIn\Client;
use LinkedIn\Scope;
// import environment variables from the environment file
// you need a .env file in the parent folder
// read this document to learn how to create that file
// https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md
//
$dotenv = new Dotenv\Dotenv(dirname(__DIR__));
$dotenv->load();
// we need a session to keep intermediate results
// you can use your own session persistence management
// client doesn't depend on it
session_start();
// instantiate the Linkedin client
// you can setup keys using
$client = new Client(
getenv('LINKEDIN_CLIENT_ID'),
getenv('LINKEDIN_CLIENT_SECRET')
);
if (isset($_GET['code'])) { // we are returning back from LinkedIn with the code
if (isset($_GET['state']) && // and state parameter in place
isset($_SESSION['state']) && // and we have have stored state
$_GET['state'] === $_SESSION['state'] // and it is our request
) {
try {
// you have to set initially used redirect url to be able
// to retrieve access token
$client->setRedirectUrl($_SESSION['redirect_url']);
// retrieve access token using code provided by LinkedIn
$accessToken = $client->getAccessToken($_GET['code']);
h1('Access token');
pp($accessToken); // print the access token content
h1('Profile');
// perform api call to get profile information
$profile = $client->get(
'me',
['fields' => 'id,firstName,lastName']
);
pp($profile); // print profile information
$emailInfo = $email = $client->get('emailAddress', ['q' => 'members', 'projection' => '(elements*(handle~))']);
pp($emailInfo);
$share = $client->post(
'ugcPosts',
[
'author' => 'urn:li:person:' . $profile['id'],
'lifecycleState' => 'PUBLISHED',
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
'shareCommentary' => [
'text' => 'Checkout this amazing PHP SDK for LinkedIn!'
],
'shareMediaCategory' => 'ARTICLE',
'media' => [
[
'status' => 'READY',
'description' => [
'text' => 'OAuth 2 flow, composer Package.'
],
'originalUrl' => 'https://github.com/zoonman/linkedin-api-php-client',
'title' => [
'text' => 'PHP Client for LinkedIn API'
]
]
]
]
],
'visibility' => [
'com.linkedin.ugc.MemberNetworkVisibility' => 'CONNECTIONS'
]
]
);
pp($share);
// set sandboxed company page id to work with
// https://www.linkedin.com/company/devtestco
/* TODO!
$companyId = '2414183';
h1('Company information');
$companyInfo = $client->get('companies/' . $companyId . ':(id,name,num-followers,description)');
pp($companyInfo);
h1('Sharing on company page');
$companyShare = $client->post(
'companies/' . $companyId . '/shares',
[
'comment' =>
sprintf(
'%s %s just tried this amazing PHP SDK for LinkedIn!',
$profile['firstName'],
$profile['lastName']
),
'content' => [
'title' => 'PHP Client for LinkedIn API',
'description' => 'OAuth 2 flow, composer Package',
'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
'submitted-image-url' => 'https://github.com/fluidicon.png',
],
'visibility' => [
'code' => 'anyone'
]
]
);
pp($companyShare);
*/
/*
// Returns {"serviceErrorCode":100,"message":"Not enough permissions to access media resource","status":403}
// You have to be whitelisted or so by LinkedIn
$filename = './demo.jpg';
$client->setApiRoot('https://api.linkedin.com/');
$mp = $client->upload($filename);
*/
} catch (\LinkedIn\Exception $exception) {
// in case of failure, provide with details
pp($exception);
pp($_SESSION);
}
echo '<a href="/">Start over</a>';
} else {
// normally this shouldn't happen unless someone sits in the middle
// and trying to override your state
// or you are trying to change saved state during linking
echo 'Invalid state!';
pp($_GET);
pp($_SESSION);
echo '<a href="/">Start over</a>';
}
} elseif (isset($_GET['error'])) {
// if you cancel during linking
// you will be redirected back with reason
pp($_GET);
echo '<a href="/">Start over</a>';
} else {
// define desired list of scopes
$scopes = [
Scope::READ_LITE_PROFILE,
Scope::READ_EMAIL_ADDRESS,
Scope::SHARE_AS_USER,
];
$loginUrl = $client->getLoginUrl($scopes); // get url on LinkedIn to start linking
$_SESSION['state'] = $client->getState(); // save state for future validation
$_SESSION['redirect_url'] = $client->getRedirectUrl(); // save redirect url for future validation
echo 'LoginUrl: <a href="'.$loginUrl.'">' . $loginUrl. '</a>';
}
/**
* Pretty print whatever passed in
*
* @param mixed $anything
*/
function pp($anything)
{
echo '<pre>' . print_r($anything, true) . '</pre>';
}
/**
* Add header
*
* @param string $h
*/
function h1($h) {
echo '<h1>' . $h . '</h1>';
}