2014-06-22 47 views
0

我正在使用codeigniter並希望獲取linkedin數據,但它不工作..它沒有錯誤,但我無法獲取數據也 我正在使用以下url中定義的代碼:如何在codeigniter中獲取linkedin數據

Linkedin php api not setting access token in codeigniter

和碼是

創建庫:

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 to identify request when it returns to us 
     $_SESSION['state'] = $params['state']; 

     // Redirect user to 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 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; 
    } 

    public function fetch($method, $resource, $body = '') { 
     $params = array('oauth2_access_token' => $_SESSION['access_token'], 
      'format' => 'json', 
     ); 

     // Need to use HTTPS 
     $url = 'https://api.linkedin.com' . $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, 
        ) 
       ) 
     ); 


     // Hocus Pocus 
     $response = file_get_contents($url, false, $context); 

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

} 

把你所有的常量的東西,在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'); 

終於讓我的控制器

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 Control Flow 
     if (isset($_GET['error'])) { 
      // LinkedIn returned an error 
      // load any error view here 
      exit; 
     } elseif (isset($_GET['code'])) { 
      // User authorized your application 
      if ($_SESSION['state'] == $_GET['state']) { 
       // Get token so you can make API calls 
       $this->linkedin->getAccessToken(); 
      } else { 

       // CSRF attack? Or did you mix up your states? 
       exit; 
      } 
     } 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 
       $this->linkedin->getAuthorizationCode(); 
      } 
     } 
     // define the 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 return an empty array of profile data 
     } 
    } 

} 

當我運行該控制器然後LinkedIn API的工作似乎與我的應用程序模式窗口名稱..但登錄後 當我試圖打印本屆會議它不apppears ...不知道這個代碼是否工作或..不能幫助

+0

codeingniter provvides一個稱爲會話庫,你應該使用模塊,詳細的文檔可以在這裏找到:會話類笨( http://ellislab.com/codeigniter/user-guide/libraries/sessions.html) – Cristian

回答

0

這應該工作:

LinkedIn類:

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 to identify request when it returns to us 
     $this->session->set_userdata('state',$params['state']); 

     // Redirect user to 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 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 
     $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); 
     return true; 
    } 

    public function fetch($method, $resource, $body = '') { 
     $params = array('oauth2_access_token' => $_SESSION['access_token'], 
      'format' => 'json', 
     ); 

     // Need to use HTTPS 
     $url = 'https://api.linkedin.com' . $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, 
        ) 
       ) 
     ); 


     // Hocus Pocus 
     $response = file_get_contents($url, false, $context); 

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

} 

檔案控制器:

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 Control Flow 
     if (isset($_GET['error'])) { 
      // LinkedIn returned an error 
      // load any error view here 
      exit; 
     } elseif (isset($_GET['code'])) { 
      // User authorized your application 
      if ($this->session->userdata('state'); == $_GET['state']) { 
       // Get token so you can make API calls 
       $this->linkedin->getAccessToken(); 
      } else { 

       // CSRF attack? Or did you mix up your states? 
       exit; 
      } 
     } else { 
      if ((empty($this->session->userdata('expires_at'))) || (time() > $this->session->userdata('expires_at'))) { 
       // Token has expired, clear the state 
       $this->session->sess_destroy(); 
      } 
      if (empty($this->session->userdata('access_token'))) { 
       // Start authorization process 
       $this->linkedin->getAuthorizationCode(); 
      } 
     } 
     // define the 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 return an empty array of profile data 
     } 
    } 

} 
相關問題