-1

I found a php script to scrape company profile pages from linkedin here https://stackoverflow.com/questions/42329819/how-can-i-scrape-linkedin-company-pages-with-curl-and-php-no-csrf-token-found-i#= I replaced the UserAgenet with my own. it successfully scrapes the company profile pages but when i try to scrape individual profile page, it gives me the following error, can anyone please give me a direction how to fix it.

Unauthorized

You must be authenticated to access this page.

Here is the Script:

<?php
function fetch_value($str, $find_start = '', $find_end = '')
{
    if ($find_start == '')
    {
        return '';
    }
    $start = strpos($str, $find_start);
    if ($start === false)
    {
        return '';
    }
    $length = strlen($find_start);
    $substr = substr($str, $start + $length);
    if ($find_end == '')
    {
        return $substr;
    }
    $end = strpos($substr, $find_end);
    if ($end === false)
    {
        return $substr;
    }
    return substr($substr, 0, $end);
}

$linkedin_login_page = "https://www.linkedin.com/uas/login";
$linkedin_ref = "https://www.linkedin.com";

$username = 'sample@gmail.com';
$password = 'sample';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $linkedin_login_page);
curl_setopt($ch, CURLOPT_REFERER, $linkedin_ref);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

$login_content = curl_exec($ch);

if(curl_error($ch)) {
  echo 'error:' . curl_error($ch);
}

$var = array(
            'isJsEnabled' => 'false',
            'source_app' => '',
            'clickedSuggestion' => 'false',
            'session_key' => trim($username),
            'session_password' => trim($password),
            'signin' => 'Sign In',
            'session_redirect' => '',
            'trk' => '',
            'fromEmail' => '');
        $var['loginCsrfParam'] = fetch_value($login_content, 'type="hidden" name="loginCsrfParam" value="', '"');
        $var['csrfToken'] = fetch_value($login_content, 'type="hidden" name="csrfToken" value="', '"');
        $var['sourceAlias'] = fetch_value($login_content, 'input type="hidden" name="sourceAlias" value="', '"');

        $post_array = array();
        foreach ($var as $key => $value)
        {
            $post_array[] = urlencode($key) . '=' . urlencode($value); 
        }
        $post_string = implode('&', $post_array);

curl_setopt($ch, CURLOPT_URL, "https://www.linkedin.com/uas/login-submit");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$store = curl_exec($ch);

if (stripos($store, "session_password-login-error") !== false)
{
    $err = trim(strip_tags(fetch_value($store, '<span class="error" id="session_password-login-error">', '</span>')));
    echo "Login error : ".$err;
}
elseif (stripos($store, 'profile-nav-item') !== false) 
{
        // curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/company/1113675/');
        curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/in/wcan01/');
        curl_setopt($ch, CURLOPT_POST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        $content = curl_exec($ch);
        curl_close($ch);

        $file = fopen("result.txt", 'w+'); // Create a new file, or overwrite the existing one.
        fwrite($file, $content);
        fclose($file);
}
else
{
    echo "unknown error";
}

?>
Wcan
  • 840
  • 1
  • 10
  • 31

1 Answers1

1

I doubt it's possible to scrape individuals, based on the fact that it took hiQ Labs months and cost a huge legal bill before a judge ordered Microsoft (just last month) to allow them to scrape LinkedIn's public profiles.

Here is an article about the ruling, and another about the complicated legal arguments.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105