title: Logging into LinkedIn with PHP curl
body: <p>I'm currently trying to login to LinkedIn, outside of their oAuth system (as it doesn't have the option for a task at hand). I'm not entirely familiar with cURL, so I may have some silly mistake somewhere.</p>
  
  <p>However I don't seem to be having any luck what so ever, even after taking a look at <a href="https://stackoverflow.com/questions/18907503/logging-in-to-linkedin-with-python-requests-sessions">similar questions</a> on stackoverflow but for python. I'm getting the error </p>
  
  <blockquote>
    <p>"<strong>Request Error</strong>
    We’re sorry, there was a problem with your request. Please make sure you have cookies enabled and try again."</p>
  </blockquote>
  
  <p>I've fetched the <code>loginCsrfParam</code> and have checked out the postfield sent (it all checks out), I've tried sending the postfields as either an array or as a string like: <code>session_key=".$username."&amp;session_pass...</code></p>
  
  <p>Here is my php code, any ideas?</p>
  
  <pre><code>&lt;?php
  class linkedInLogin {
      public function login(){
          $username = 'Email Here';
          $password = 'Password here';
          $csrf = $this-&gt;fetchCSRF();
          if($csrf == false){
              echo 'Error: loginCsrfParam fetching failed.';
              return;
          }
          //login form action url
          $url="https://www.linkedin.com/uas/login-submit";
          //$postinfo = "session_key=".$username."&amp;session_password=".$password."&amp;loginCsrfParam=".$csrf;
          $postfields = array('session_key' =&gt; $username, 'session_password' =&gt; $password, 'loginCsrfParam' =&gt; $csrf);
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_HEADER, false);
          curl_setopt($ch, CURLOPT_NOBODY, false);
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_COOKIESESSION, true);
          curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
          curl_setopt($ch, CURLOPT_USERAGENT,
              "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //@TODO change back: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_REFERER, 'https://www.linkedin.com/uas/login');
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
          echo curl_exec($ch);
          curl_close($ch);
      }
  
      // Fetches the CSRF for login authentication
      private function fetchCSRF(){
          $url = 'https://linkedin.com/uas/login';
          $html = file_get_contents($url);
          $precsrf = (int) strpos($html, '&lt;input type="hidden" name="loginCsrfParam" value="');
          $postcsrf = (int) strpos($html, '" id="loginCsrfParam-login"&gt;');
          $length = $postcsrf - $precsrf - 50; // The -50 / + 50 is to correct for: &lt;input type="hidden" name="login...
          $csrf = substr($html, $precsrf + 50, $length);
          if($csrf == false){
              return false;
          }
          return $csrf;
      }
  }
  </code></pre>
  
  <p><strong>Update</strong>: I still haven't had any luck with this, although other services don't offer similar functionality that I require. </p>