title: how to fetch linkedin data in codeigniter
body: <p>I am using codeigniter and want to fetch linkedin data but it not works.. it getting no error but i could not fetch data also
  i am using this code defined in following url: </p>
  
  <p><a href="https://stackoverflow.com/questions/20497889/linkedin-php-api-not-setting-access-token-in-codeigniter">Linkedin php api not setting access token in codeigniter</a></p>
  
  <p>and codes are</p>
  
  <p>create library:</p>
  
  <pre><code>defined('BASEPATH') OR exit('No direct script access allowed');
  
  
  class Linkedin {
  
      function __construct(){
  
      }
  
      public function getAuthorizationCode() {
          $params = array('response_type' =&gt; 'code',
              'client_id' =&gt; API_KEY,
              'scope' =&gt; SCOPE,
              'state' =&gt; uniqid('', true), // unique long string
              'redirect_uri' =&gt; 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' =&gt; 'authorization_code',
              'client_id' =&gt; API_KEY,
              'client_secret' =&gt; API_SECRET,
              'code' =&gt; $_GET['code'],
              'redirect_uri' =&gt; 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' =&gt;
                      array('method' =&gt; '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-&gt;access_token; // guard this! 
          $_SESSION['expires_in'] = $token-&gt;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' =&gt; $_SESSION['access_token'],
              'format' =&gt; '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' =&gt;
                      array('method' =&gt; $method,
                      )
                  )
          );
  
  
          // Hocus Pocus
          $response = file_get_contents($url, false, $context);
  
          // Native PHP object, please
          return json_decode($response);
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Put all your Constants stuff in confin/constants.php</p>
  
  <pre><code>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');
  </code></pre>
  </blockquote>
  
  <p>and finally make my controller</p>
  
  <pre><code>class Profile extends CI_Controller {
  
      function __construct() {
          parent:: __construct();
          $this-&gt;load-&gt;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-&gt;linkedin-&gt;getAccessToken();
              } else {
  
                  // CSRF attack? Or did you mix up your states?
                  exit;
              }
          } else {
              if ((empty($_SESSION['expires_at'])) || (time() &gt; $_SESSION['expires_at'])) {
                  // Token has expired, clear the state
                  $_SESSION = array();
              }
              if (empty($_SESSION['access_token'])) {
                  // Start authorization process
                  $this-&gt;linkedin-&gt;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-&gt;linkedin-&gt;fetch('GET', '/v1/people/~:(' . implode(',', $profile_fileds) . ')');
          if ($profileData) {
            $this-&gt;session-&gt;set_userdata("profile_session",$profileData);
          } else {
             // linked return an empty array of profile data
          }
      }
  
  }
  </code></pre>
  
  <p>When i am running this controller then linkedin api works a modal window appears with my app name ..but after login
  When i trying to print this session it does not apppears... dont know this code is working or not.. please help</p>