6

I have a PHP script which opens http requests by using CURL: (it also accepts header if it is needed)

   $c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
if ($post_paramtrs) {
    curl_setopt($c, CURLOPT_POST, TRUE);
    curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&" . $post_paramtrs);
}
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
curl_setopt($c, CURLOPT_COOKIEJAR, $dirname . 'cookief.txt');
curl_setopt($c, CURLOPT_COOKIEFILE, $dirname . 'cookief.txt');
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
$follow_allowed = (ini_get('open_basedir') || ini_get('safe_mode')) ? false : true;
if ($follow_allowed) {
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
}
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
$data = curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);

It works as well. Now, I want to get the result of linkedin's search. Here is the page you can search. As you see it sends an ajax request to get the data. For example, if you want to search for Peter, it sends this request:

https://www.linkedin.com/voyager/api/typeahead/hits?q=blended&query=Peter

But when you open it manually, it fails and throws this error:

CSRF check failed.

It means I have to pass this token with along the request:

enter image description here


What's My question? How can I find that token? Noted that it isn't exist in the DOM. Will it be created by JS? Anyway, do you have any clue for me?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

1 Answers1

0

Search API isn't available for anonymous users, so you need to login and get a valid authentication cookie before executing this request.

Login and capture token from cookies: li_at

Finally, pass the cookie and CSRF token like this:

GET https://www.linkedin.com/voyager/api/typeahead/hits?q=blended&query=Peter 

cookie: JSESSIONID=NotImportant;li_at={GRAB_IT_FROM_COOKIE};
csrf-token: NotImportant

LinkedIn server will check JSESSIONID and csrf-token to be equal, so its value isn't important.

You can easily add a custom header to request with CURLOPT_HTTPHEADER in curl

Pouya Darabi
  • 2,246
  • 18
  • 23