View difference between Paste ID: c7XPgZPs and FLBc2fbS
SHOW: | | - or go back to the newest paste.
1
<?php
2
// Change these
3
define('API_KEY',      'YOUR_API_KEY_HERE'                                          );
4
define('API_SECRET',   'YOUR_API_SECRET_HERE'                                       );
5
define('REDIRECT_URI', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']);
6-
define('SCOPE',        'r_fullprofile r_emailaddress rw_nus'   
6+
define('SCOPE',        'r_fullprofile r_emailaddress rw_nus'   			    );
7
8
// You'll probably use a database
9
session_name('linkedin');
10
session_start();
11
12
// OAuth 2 Control Flow
13
if (isset($_GET['error'])) {
14
	// LinkedIn returned an error
15
	print $_GET['error'] . ': ' . $_GET['error_description'];
16
	exit;
17
} elseif (isset($_GET['code'])) {
18
	// User authorized your application
19
	if ($_SESSION['state'] == $_GET['state']) {
20
		// Get token so you can make API calls
21
		getAccessToken();
22
	} else {
23
		// CSRF attack? Or did you mix up your states?
24
		exit;
25
	}
26
} else { 
27
	if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
28
		// Token has expired, clear the state
29
		$_SESSION = array();
30
	}
31
	if (empty($_SESSION['access_token'])) {
32
		// Start authorization process
33
		getAuthorizationCode();
34
	}
35
}
36
37
// Congratulations! You have a valid token. Now fetch your profile 
38-
$user = fetch('GET', '/v1/people/~:(certifications:(authority:(name),name,number,start-date,end-date),patents:(title,summary,inventors:(person),date,office:(name),number),languages:(language:(name),proficiency:(name)),num-recommenders,recommendations-received,main-address,summary,date-of-birth,interests,id,first-name,last-name,headline,picture-url,email-address,location:(name),industry,positions,skills,volunteer,educations,publications:(authors:(name),title,date,url,summary,publisher:(name)),phone-numbers)');
38+
$user = fetch('GET', '/v1/people/~:(network,honors-awards,certifications:(authority:(name),name,number,start-date,end-date),patents:(title,summary,inventors:(person),date,office:(name),number),languages:(language:(name),proficiency:(name)),num-recommenders,recommendations-received,main-address,summary,date-of-birth,interests,id,first-name,last-name,headline,picture-url,email-address,location:(name),industry,positions,skills,volunteer,educations,publications:(authors:(name),title,date,url,summary,publisher:(name)),phone-numbers)');
39
print "Hello $user->firstName $user->lastName.";
40
echo "<pre>";var_dump($user);
41
echo "</pre>";
42
exit;
43
44
function getAuthorizationCode() {
45
	$params = array('response_type' => 'code',
46
					'client_id' => API_KEY,
47
					'scope' => SCOPE,
48
					'state' => uniqid('', true), // unique long string
49
					'redirect_uri' => REDIRECT_URI,
50
			  );
51
52
	// Authentication request
53
	$url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
54
	
55
	// Needed to identify request when it returns to us
56
	$_SESSION['state'] = $params['state'];
57
58
	// Redirect user to authenticate
59
	header("Location: $url");
60
	exit;
61
}
62
	
63
function getAccessToken() {
64
	$params = array('grant_type' => 'authorization_code',
65
					'client_id' => API_KEY,
66
					'client_secret' => API_SECRET,
67
					'code' => $_GET['code'],
68
					'redirect_uri' => REDIRECT_URI,
69
			  );
70
	
71
	// Access Token request
72
	$url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
73
	
74
	// Tell streams to make a POST request
75
	$context = stream_context_create(
76
					array('http' => 
77
						array('method' => 'POST',
78
	                    )
79
	                )
80
	            );
81
82
	// Retrieve access token information
83
	$response = file_get_contents($url, false, $context);
84
85
	// Native PHP object, please
86
	$token = json_decode($response);
87
88
	// Store access token and expiration time
89
	$_SESSION['access_token'] = $token->access_token; // guard this! 
90
	$_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
91
	$_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
92
	
93
	return true;
94
}
95
96
function fetch($method, $resource, $body = '') {
97
	$params = array('oauth2_access_token' => $_SESSION['access_token'],
98
					'format' => 'json',
99
			  );
100
	
101
	// Need to use HTTPS
102
	$url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
103
	// Tell streams to make a (GET, POST, PUT, or DELETE) request
104
	$context = stream_context_create(
105
					array('http' => 
106
						array('method' => $method,
107
	                    )
108
	                )
109
	            );
110
111
112
	// Hocus Pocus
113
	$response = file_get_contents($url, false, $context);
114
115
	// Native PHP object, please
116
	return json_decode($response);
117
}