php - how to fetch linkedin data in codeigniter -
i using codeigniter , want fetch linkedin info not works.. getting no error not fetch info using code defined in next url:
linkedin php api not setting access token in codeigniter
and codes are
create library:
defined('basepath') or exit('no direct script access allowed'); class linkedin { function __construct(){ } public 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?' . http_build_query($params); // needed identify request when returns $_session['state'] = $params['state']; // redirect user authenticate header("location: $url"); exit; } public 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?' . http_build_query($params); // tell streams create post request $context = stream_context_create( array('http' => array('method' => 'post', ) ) ); // retrieve access token info $response = file_get_contents($url, false, $context); // native php object, please $token = json_decode($response); // store access token , 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 homecoming true; } public function fetch($method, $resource, $body = '') { $params = array('oauth2_access_token' => $_session['access_token'], 'format' => 'json', ); // need utilize https $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); // tell streams create (get, post, put, or delete) request $context = stream_context_create( array('http' => array('method' => $method, ) ) ); // hocus pocus $response = file_get_contents($url, false, $context); // native php object, please homecoming json_decode($response); } }
put constants stuff in confin/constants.php
define('api_key', 'put yoour api_key here'); define('api_secret', 'put yoour api_secret here'); define('redirect_uri', 'put yoour redirect_uri here'); define('scope', 'r_fullprofile r_emailaddress rw_nus r_contactinfo r_network');
and create controller
class profile extends ci_controller { function __construct() { parent:: __construct(); $this->load->library('linkedin'); // load library session_name('linkedin'); session_start(); } // linkedin login script function profile() { // oauth 2 command flow if (isset($_get['error'])) { // linkedin returned error // load error view here exit; } elseif (isset($_get['code'])) { // user authorized application if ($_session['state'] == $_get['state']) { // token can create api calls $this->linkedin->getaccesstoken(); } else { // csrf attack? or did mix states? exit; } } else { if ((empty($_session['expires_at'])) || (time() > $_session['expires_at'])) { // token has expired, clear state $_session = array(); } if (empty($_session['access_token'])) { // start authorization process $this->linkedin->getauthorizationcode(); } } // define array of profile fields $profile_fileds = array( 'id', 'firstname', 'maiden-name', 'lastname', 'picture-url', 'email-address', 'location:(country:(code))', 'industry', 'summary', 'specialties', 'interests', 'public-profile-url', 'last-modified-timestamp', 'num-recommenders', 'date-of-birth', ); $profiledata = $this->linkedin->fetch('get', '/v1/people/~:(' . implode(',', $profile_fileds) . ')'); if ($profiledata) { $this->session->set_userdata("profile_session",$profiledata); } else { // linked homecoming empty array of profile info } } }
when running controller linkedin api works modal window appears app name ..but after login when trying print session not apppears... dont know code working or not.. please help
this should work:
linkedin class:
class linkedin extends ci_controller { function __construct(){ parent:: __construct(); $this->load->library('session'); } public 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?' . http_build_query($params); // needed identify request when returns $this->session->set_userdata('state',$params['state']); // redirect user authenticate header("location: $url"); exit; } public 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?' . http_build_query($params); // tell streams create post request $context = stream_context_create( array('http' => array('method' => 'post', ) ) ); // retrieve access token info $response = file_get_contents($url, false, $context); // native php object, please $token = json_decode($response); // store access token , expiration time $ses_params = array('access_token' => $token->access_token, 'expires_in' => $token->expires_in, 'expires_at' => time() + $_session['expires_in']); $this->session->set_userdata($ses_params); homecoming true; } public function fetch($method, $resource, $body = '') { $params = array('oauth2_access_token' => $_session['access_token'], 'format' => 'json', ); // need utilize https $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); // tell streams create (get, post, put, or delete) request $context = stream_context_create( array('http' => array('method' => $method, ) ) ); // hocus pocus $response = file_get_contents($url, false, $context); // native php object, please homecoming json_decode($response); } }
profile controller:
class profile extends ci_controller { function __construct() { parent:: __construct(); $this->load->library('linkedin'); // load library $this->load->library('session'); } // linkedin login script function profile() { // oauth 2 command flow if (isset($_get['error'])) { // linkedin returned error // load error view here exit; } elseif (isset($_get['code'])) { // user authorized application if ($this->session->userdata('state'); == $_get['state']) { // token can create api calls $this->linkedin->getaccesstoken(); } else { // csrf attack? or did mix states? exit; } } else { if ((empty($this->session->userdata('expires_at'))) || (time() > $this->session->userdata('expires_at'))) { // token has expired, clear state $this->session->sess_destroy(); } if (empty($this->session->userdata('access_token'))) { // start authorization process $this->linkedin->getauthorizationcode(); } } // define array of profile fields $profile_fileds = array( 'id', 'firstname', 'maiden-name', 'lastname', 'picture-url', 'email-address', 'location:(country:(code))', 'industry', 'summary', 'specialties', 'interests', 'public-profile-url', 'last-modified-timestamp', 'num-recommenders', 'date-of-birth', ); $profiledata = $this->linkedin->fetch('get', '/v1/people/~:(' . implode(',', $profile_fileds) . ')'); if ($profiledata) { $this->session->set_userdata("profile_session",$profiledata); } else { // linked homecoming empty array of profile info } } }
php codeigniter linkedin
No comments:
Post a Comment