title: How do I retrieve the entire users profile (linkedin api)
body: <p>Here is my current API Query:</p>
  
  <pre><code>https://api.linkedin.com/v1/people/~:(firstName,lastName)
  </code></pre>
  
  <p>However this only returns the first and last name. </p>
  
  <p>My scope is: </p>
  
  <pre><code>r_fullprofile r_contactinfo r_emailaddress
  </code></pre>
  
  <p>How do I retrieve the entire users profile?</p>
  
  <p>Here is my current code: </p>
  
  <pre><code>        &lt;?php
          // Change these
          define('API_KEY',      'xxx'                                          );
          define('API_SECRET',   'xxx'                                       );
          define('REDIRECT_URI', 'xxx');
          define('SCOPE',        'r_fullprofile r_contactinfo r_emailaddress');
  
          // You'll probably use a database
          session_name('linkedin');
          session_start();
  
          // OAuth 2 Control Flow
          if (isset($_GET['error'])) {
              // LinkedIn returned an error
              print $_GET['error'] . ': ' . $_GET['error_description'];
              exit;
          } elseif (isset($_GET['code'])) {
              // User authorized your application
              if ($_SESSION['state'] == $_GET['state']) {
                  // Get token so you can make API calls
                  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
                  getAuthorizationCode();
              }
          }
  
          // Congratulations! You have a valid token. Now fetch your profile 
          $user = fetch('GET', '/v1/people/~:(firstName,lastName)');
          print_r($user);
          print "Hello $user-&gt;firstName $user-&gt;lastName.";
          exit;
  
          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;
          }
  
          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;
          }
  
          function fetch($method, $resource, $body = '') {
              print $_SESSION['access_token'];
  
              $opts = array(
                  'http'=&gt;array(
                      'method' =&gt; $method,
                      'header' =&gt; "Authorization: Bearer " . $_SESSION['access_token'] . "\r\n" . "x-li-format: json\r\n"
                  )
              );
  
              // Need to use HTTPS
              $url = 'https://api.linkedin.com' . $resource;
  
              // Append query parameters (if there are any)
              if (count($params)) { $url .= '?' . http_build_query($params); }
  
              // Tell streams to make a (GET, POST, PUT, or DELETE) request
              // And use OAuth 2 access token as Authorization
              $context = stream_context_create($opts);
  
              // Hocus Pocus
              $response = file_get_contents($url, false, $context);
  
              // Native PHP object, please
              return json_decode($response);
          }
  </code></pre>