Linked in Integration using php codeigniter

First We need to have linkedin Account in Linked in website

After Creating Account goto https://www.linkedin.com/developer/apps/ page then create application

Then use below php code to access your linked in posts..

[code language="PHP"]
$aSettings = array(
'api_key'      => "xxxxxxxxxxxxxxxx",
'api_secret'   => "xxxxxxxxxxx",
'scope'        => 'rw_company_admin',
'redirect_uri' => 'redirect uri specified in linkedin oauth app',

);
//$aSettings = array_merge($aSettings, $aParams);

/* Set variables */
$this->sApikey      = $aSettings['api_key'];
$this->sApisecret   = $aSettings['api_secret'];
$this->sScope       = $aSettings['scope'];
$this->sRedirectUri = $aSettings['redirect_uri'];

}
public function index(){
if(!isset($_SESSION["access_token"])){
$this->oAuth();
}
$data=array("oauth2_access_token"=>$_SESSION["access_token"],"format"=>"json");
echo "

";
print_r($this->company("13210084",$data));echo "
";

}

public function company($iCompanyID, $aSettings = array())
{
$aOutput = $this->fetch('GET', 'companies/' . $iCompanyID . '/updates', $aSettings);

return $aOutput;
}

public function getErrors($sType = 'array')
{
switch ($sType) {
case 'html':
$sOutput = '

';
$sOutput .= 'The LinkedIn API failed to connect due to the following error(s):';
$sOutput .= '
';
foreach ($this->aErrors as $i => $sError) {
$sOutput .= '
' . $sError . '
';
}
$sOutput .= '
';
return $sOutput;
case 'array':
default:
return $this->aErrors;
}
}
private function setError($sString)
{
$this->aErrors[] = $sString;
}

/* The actual fetch */
private function fetch($sMethod, $sLink, $aParams = array())
{

if ($_SESSION['access_code']) {
$aHeaders = array(
'Authorization' => 'Bearer ' . $_SESSION['access_token'],
'x-li-format'   => 'json', // Comment out to use XML
);
}

// Need to use HTTPS
$sUrl = trim(self::API_LINK, '/') . '/' . trim($sLink, '/');

// Append query parameters (if there are any)
if (count($aParams)) {
$sUrl .= '?' . http_build_query($aParams);
}

// Tell streams to make a (GET, POST, PUT, or DELETE) request
// And use OAuth 2 access token as Authorization
$context = stream_context_create(
array('http' => array('method' => $sMethod,
'header'                       => (isset($aHeaders)) ? $aHeaders : null,
),
)
);
// Hocus Pocus
$sResponse = $this->processCurl($sUrl);

// Native PHP object, please
return json_decode($sResponse);
}

/* Authorisation handler */
public function oAuth()
{
// OAuth 2 Control Flow
if (isset($_GET['error'])) {
$this->setError(trim(htmlentities($_GET['error'] . ': ' . $_GET['error_description'])));
return false;
} elseif (isset($_GET['code'])) {
// User authorized your application
if ($this->getState() == $_GET['state']) {
// Get token so you can make API calls
if ($this ->getAccessToken()) {
return true;
}
} else {
print_r('Possible CRFS attack. Wrong state code: "' . $this->getState() . '" != "' . $_GET['state'] . '"', 2);
$this->setError('Login to Linkedin failed because an attack was detected.');
return false;
}
} else {
if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
// Token has expired, clear the state
$_SESSION = array();
}
if (!isset($_SESSION['access_token']) || empty($_SESSION['access_token'])) {
// Start authorization process
$this->getAuthorisation();
}
}
}

/* Get Authorisation code */
private function getAuthorisation()
{
$aParams = array(
'response_type' => 'code',
'client_id'     => $this->sApikey,
'scope'         => $this->sScope,
'state'         => $this->getState(),
'redirect_uri'  => $this->sRedirectUri,
);

// Authentication request
$sUrl = self::AUTH_LINK . http_build_query($aParams);

// Redirect user to authenticate
header("Location: $sUrl");
exit();
}

/* Get access token */
public function getAccessToken()
{
$aParams = array(
'grant_type'    => 'authorization_code',
'client_id'     => $this->sApikey,
'client_secret' => $this->sApisecret,
'code'          => $_GET['code'],
'redirect_uri'  => $this->sRedirectUri,
);

// Access Token request
$sUrl = self::ACC_LINK . http_build_query($aParams);

// Tell streams to make a POST request
$sContext = stream_context_create(
array('http' => array('method' => 'POST'))
);

// Retrieve access token information
$sResponse = file_get_contents($sUrl, false, $sContext);

// Native PHP object, please
$oToken = json_decode($sResponse);

// Store access token and expiration time
$_SESSION['access_token'] = $oToken->access_token; // guard this!
$_SESSION['expires_in']   = $oToken->expires_in; // relative time (in seconds)
$_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time

redirect(base_url("linkedin/index"));
}

private function getState()
{
if (is_null($this->sScope)) {
if (!isset($_SESSION['ln-api-scope'])) {
$_SESSION['ln-api-scope'] = md5(uniqid(rand(), true));
}
$this->sScope = $_SESSION['ln-api-scope'];
}
return $this->sScope;
}
public function logout(){
$_SESSION=array();
}
public function processCurl($URL)
{
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$resulta = curl_exec ($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
return json_encode($resulta,true);
}
}
[/code]

Comments

Popular posts from this blog

Simple Mail Sending Contact Form using PHP and Jquery

Codeigniter with Admin LTE

onclick anchor tag open url in bootstrap model