Tuesday, September 24, 2013

USING LINKEDIN API TO FETCH RECORDS (FULL PROFILE, CONNECTIONS, ETC)

Hiii, You all know that to allow the user to connect on Linkedin, Facebook or Gmail, etc on your site, you require an API. So,here in this post I will teach you how to connect and fetch the data from any Linkedin account with the help of linkedin API. This functionality is required to add a “Connect To Linkedin” button on your site to directly make the user visiting your site to directly connect to linkedin .
Through this tutorial, you can fetch connections, profile details, job updates, etc from Linkedin profile after login. In this tutorial I am fetching the full profile details of the person who is logging in but you can fetch details according to your needs by changing scope details from “r_fullprofile” to any other like "r_network". Information about scope is present on Linkedin Developer site
To run this tutorial firstly you need an API KEY and Secret API , which you can get very easily by registering your site/application to linkedin.

CODE BEGINS :

<?php
// Change these
define(‘API_KEY’,      ‘your api key here’                                          );
define(‘API_SECRET’,   ‘your secret key here’                                       );
define(‘REDIRECT_URI’, ‘your redirect url’);              //in our case, redirect it on this page
define(‘SCOPE’,        ‘r_fullprofile r_emailaddress rw_nus’     );
// You’ll probably use a database
session_name(‘linkedin’);
session_start();
//$_SESSION['state']=”India”;
// OAuth 2 Control Flow
if (isset($_GET['error'])) {
// LinkedIn returned an error
print $_GET['error'] . ‘: ‘ . $_GET['error_description'];
exit;
} elseif (isset($_GET['code'])) {
// User authorized your application
// Get token so you can make API calls
getAccessToken();
} else {
if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
// Token has expired, clear the state
$_SESSION = array();
}
if (empty($_SESSION['access_token'])) {
// Start authorization process
getAuthorizationCode();
}
}
// Congratulations! You have a valid token. Now fetch your profile
$user = fetch(‘GET’, ‘/v1/people/~:(id,firstName,lastName,skills,headline,location,industry,email-address,network)’);
//print “Hello $user->firstName $user->lastName.”;
echo “<pre>”;
print_r($user);
echo “<pre>”;
exit;
function getAuthorizationCode() {
$params = array(‘response_type’ => ‘code’,
‘client_id’ => API_KEY,
‘scope’ => SCOPE,
‘state’ => uniqid(”, true), // unique long string
‘redirect_uri’ => REDIRECT_URI,
);
// Authentication request
$url = ‘https://www.linkedin.com/uas/oauth2/authorization?&#8217; . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['state'] = $params['state'];
// Redirect user to authenticate
header(“Location: $url”);
exit;
}
function getAccessToken() {
$params = array(‘grant_type’ => ‘authorization_code’,
‘client_id’ => API_KEY,
‘client_secret’ => API_SECRET,
‘code’ => $_GET['code'],
‘redirect_uri’ => REDIRECT_URI,
);
// Access Token request
$url = ‘https://www.linkedin.com/uas/oauth2/accessToken?&#8217; . http_build_query($params);
// Tell streams to make a POST request
$context = stream_context_create(
array(‘http’ =>
array(‘method’ => ‘POST’,
)
)
);
// Retrieve access token information
$response = file_get_contents($url, false, $context);
// Native PHP object, please
$token = json_decode($response);
// Store access token and expiration time
$_SESSION['access_token'] = $token->access_token; // guard this!
$_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
$_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
return true;
}
function fetch($method, $resource, $body = ”) {
$params = array(‘oauth2_access_token’ => $_SESSION['access_token'],
‘format’ => ‘json’,
);
// Need to use HTTPS
$url = ‘https://api.linkedin.com&#8217; . $resource . ‘?’ . http_build_query($params);
// Tell streams to make a (GET, POST, PUT, or DELETE) request
$context = stream_context_create(
array(‘http’ =>
array(‘method’ => $method,
)
)
);
$response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response);
}
When you run this code, Linkedin authorization screen will open as shown below :
linkedin_api

Hope you like this post……..Please Comment....!!!!!!
Originally posted at Mycodestock.


Tags: ,

0 Responses to “USING LINKEDIN API TO FETCH RECORDS (FULL PROFILE, CONNECTIONS, ETC)”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks