CodeIgniter : Import Linkedin data
A while back I had a CodeIgniter project that involved importing Linkedin data. In this tutorial, we will try to :
- Authenticate into LinkedIn with OAuth
- Extract data from Linkedin profile
This tutorial will only show you the imported data as output.....how you want to use the data is completely up to you. ;-)
To begin, download the OAuth.php
and linkedin.php
files from http://code.google.com/p/simple-linkedinphp/downloads/list
After unzip, copy the OAuth.php
and linkedin_3.2.0.class.php
into application/libraries
folder.
Rename linkedin_3.2.0.class.php
to linkedin.php
Follow the instruction at https://developer.linkedin.com/documents/authentication to get your appKey and appSecret
Create this CodeIgniter controller: - linkedin_singup.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Linkedin_signup extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index() {
echo '<form id="linkedin_connect_form" action="linkedin_signup/initiate" method="post">';
echo '<input type="submit" value="Login with LinkedIn" />';
echo '</form>';
}
function initiate() {
// setup before redirecting to Linkedin for authentication.
$linkedin_config = array(
'appKey' => '<your app key here >',
'appSecret' => '<your app secret here >',
'callbackUrl' => '<your URL here>/linkedin_signup/data/');
$this->load->library('linkedin', $linkedin_config);
$this->linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
$token = $this->linkedin->retrieveTokenRequest();
$this->session->set_flashdata('oauth_request_token_secret', $token['linkedin']['oauth_token_secret']);
$this->session->set_flashdata('oauth_request_token', $token['linkedin']['oauth_token']);
$link = "https://api.linkedin.com/uas/oauth/authorize?oauth_token=" . $token['linkedin']['oauth_token'];
redirect($link);
}
function cancel() {
// See https://developer.linkedin.com/documents/authentication
// You need to set the 'OAuth Cancel Redirect URL' parameter to <your URL>/linkedin_signup/cancel
echo 'Linkedin user cancelled login';
}
function logout() {
session_unset();
$_SESSION = array();
echo "Logout successful";
}
function data() {
$linkedin_config = array(
'appKey' => '<your app key here >',
'appSecret' => '<your app secret here >',
'callbackUrl' => '<your URL here>/linkedin_signup/data/'
);
$this->load->library('linkedin', $linkedin_config);
$this->linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
$oauth_token = $this->session->flashdata('oauth_request_token');
$oauth_token_secret = $this->session->flashdata('oauth_request_token_secret');
$oauth_verifier = $this->input->get('oauth_verifier');
$response = $this->linkedin->retrieveTokenAccess($oauth_token, $oauth_token_secret, $oauth_verifier);
// ok if we are good then proceed to retrieve the data from Linkedin
if ($response['success'] === TRUE) {
// From this part onward it is up to you on how you want to store/manipulate the data
$oauth_expires_in = $response['linkedin']['oauth_expires_in'];
$oauth_authorization_expires_in = $response['linkedin']['oauth_authorization_expires_in'];
$response = $this->linkedin->setTokenAccess($response['linkedin']);
$profile = $this->linkedin->profile('~:(id,first-name,last-name,picture-url)');
$profile_connections = $this->linkedin->profile('~/connections:(id,first-name,last-name,picture-url,industry)');
$user = json_decode($profile['linkedin']);
$user_array = array('linkedin_id' => $user->id, 'second_name' => $user->lastName, 'profile_picture' => $user->pictureUrl, 'first_name' => $user->firstName);
// For example, print out user data
echo 'User data:';
print '<pre>';
print_r($user_array);
print '</pre>';
echo '<br><br>';
// Example of company data
$company = $this->linkedin->company('1337:(id,name,ticker,description,logo-url,locations:(address,is-headquarters))');
echo 'Company data:';
print '<pre>';
print_r($company);
print '</pre>';
echo '<br><br>';
echo 'Logout';
echo '<form id="linkedin_connect_form" action="../logout" method="post">';
echo '<input type="submit" value="Logout from LinkedIn" />';
echo '</form>';
} else {
// bad token request, display diagnostic information
echo "Request token retrieval failed:<br /><br />RESPONSE:<br /><br />" . print_r($response, TRUE);
}
}
}
?>
If you want to get additional information like email address from Linkedin
Modify line 140 in linkedin.php file
Add new scope to the requestToken
const URLREQUEST = 'https://api.linkedin.com/uas/oauth/requestToken';
to
const URLREQUEST = 'https://api.linkedin.com/uas/oauth/requestToken?scope=rfullprofile+rcontactinfo+r_emailaddress';
then add email-address
:
$profile = $this->linkedin->profile(’~:(id,first-name,last-name,picture-url,headline,location,<strong>email-address</strong>)’);
$user = json_decode($profile[‘linkedin’]);
$user_array = array(‘linkedin_id’ => $user->id,
‘second_name’ => $user->lastName,
‘profile_picture’ => $user->pictureUrl,
‘first_name’ => $user->firstName,
‘headline’ => $user->headline,
‘location’ => $user->location,
‘email-address’ => $user->emailAddress); //change email-address to emailAddress
The list of available scopes are documented in http://developer.linkedin.com/documents/authentication under "Below is a list of all member permissions"
Click here to download the source code
Notes :
This solution was first posted to the CodeIgniter community forum on 19th October 2012
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.2k Mac OSX : Get a process/daemon status information
+17.7k Golang : Get all upper case or lower case characters from string example
+17.5k Golang : Iterate linked list example
+11k Golang : How to pipe input data to executing child process?
+6.9k Golang : Get Alexa ranking data example
+16.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+29.2k Golang : Login(Authenticate) with Facebook example
+11.4k Golang : How to detect a server/machine network interface capabilities?
+8.8k Golang : How to capture return values from goroutines?
+16.1k Golang : Find out mime type from bytes in buffer
+6.4k Golang : Join lines with certain suffix symbol example
+33.7k Golang : Create x509 certificate, private and public keys