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
+8.7k Golang : Detect number of faces or vehicles in a photo
+6k Golang : Trim everything onward after a word
+4.8k Golang : Find change in a combination of coins example
+3.7k JavaScript: Add marker function on Google Map
+5.6k Golang : Warp text string by number of characters or runes example
+12.4k Golang : Check if an integer is negative or positive
+8.2k Golang : Changing a RGBA image number of channels with OpenCV
+17.7k Golang : Get RGBA values of each image pixel
+5.7k Golang : How to fix html/template : "somefile" is undefined error?
+19.8k Golang : GORM create record or insert new record into database example
+3.9k MariaDB/MySQL : Form select statement or search query with Chinese characters
+5.6k PHP : Shuffle to display different content or advertisement