Search code examples
phpcurllinkedin-apiaccess-token

LinkedIn Get Token Access via PHP cUrl Fail


I try to create a register function with linkedIn in my website.

As you know, linkedIn has 2 step authentication to get Access token.

See StackOverflow answer here : Click here

From the thread above, i try to do the second step with PHP cUrl.

$url = "https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code=" . $_GET['code'] . "&redirect_uri=A_URL&client_id=MY_CLIENT_ID&client_secret=SECRET";
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL            => $url,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST  => "POST",
  CURLOPT_TIMEOUT        => 1000,
  CURLOPT_HTTPHEADER     => array(
    "Content-Length: 1000" 
  )
));

$response = curl_exec($curl);
$err      = curl_error($curl);

curl_close($curl);

if (!$err) {
  $response = json_decode($response);
} else {
  throw new exception(__METHOD__ . '() ' . $err . ', on line : ' . __LINE__);
}

but when the curl_exec() is executed, the page goes to loading infinitely.

I do not know what's my mistake. I try to run the $url manually via the url box, it return a token and expired time successfully. So, i conclude the mistake is in the cUrl.

Any help guys ?,

Thank you very much.


Solution

  • CURLOPT_CUSTOMREQUEST => "GET"

    and delete CURLOPT_HTTPHEADER option, It's useless.