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.
However I don't seem to be having any luck what so ever, even after taking a look at similar questions on stackoverflow but for python. I'm getting the error
"Request Error We’re sorry, there was a problem with your request. Please make sure you have cookies enabled and try again."
I've fetched the loginCsrfParam
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: session_key=".$username."&session_pass...
Here is my php code, any ideas?
<?php
class linkedInLogin {
public function login(){
$username = 'Email Here';
$password = 'Password here';
$csrf = $this->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."&session_password=".$password."&loginCsrfParam=".$csrf;
$postfields = array('session_key' => $username, 'session_password' => $password, 'loginCsrfParam' => $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, '<input type="hidden" name="loginCsrfParam" value="');
$postcsrf = (int) strpos($html, '" id="loginCsrfParam-login">');
$length = $postcsrf - $precsrf - 50; // The -50 / + 50 is to correct for: <input type="hidden" name="login...
$csrf = substr($html, $precsrf + 50, $length);
if($csrf == false){
return false;
}
return $csrf;
}
}
Update: I still haven't had any luck with this, although other services don't offer similar functionality that I require.