Created
September 21, 2017 08:02
-
-
Save makgithub/8fd40e19911111e5bd698907f5a3a9ef to your computer and use it in GitHub Desktop.
LinkedIn API client in PHP to get Access Token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(-1); | |
echo " testing"; | |
/** | |
* | |
*The below is the url to get the Authorization code using our LinkedIn App Details | |
*/ | |
$url="https://www.linkedin.com/". | |
'oauth/v2/authorization?response_type=code&client_id=81vfvatntv3k0&' | |
. 'redirect_uri=http://localhost/servers/link.php&state=DCEeFWf45A53sdfKef424'; | |
$code=$_GET['code']; | |
if(isset($code)){ | |
$myUrl = "https://www.linkedin.com/oauth/v2/accessToken"; | |
$myData = array( | |
'grant_type' => 'authorization_code', | |
'code' => $code, | |
'redirect_uri' => '********',//Redirect Url | |
'client_id'=>'CLINETId',//Linkedin App Client Id | |
'client_secret'=>'******'//Linkedin App Secret Key | |
); | |
$myHeaders = array( | |
'Content-type: application/x-www-form-urlencoded' | |
); | |
$handle = curl_init(); | |
curl_setopt($handle, CURLOPT_URL, $myUrl); | |
curl_setopt($handle, CURLOPT_HTTPHEADER, $myHeaders); | |
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($handle, CURLOPT_POST, true); | |
//curl_setopt($handle, CURLOPT_POSTFIELDS, $myData); | |
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($myData)); | |
$response = curl_exec($handle); | |
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE); | |
print "return code = $code <br/>"; | |
print "response = <br/> $response <br/><br/>"; | |
$myRespArr = json_decode($response, true); | |
print "<pre>"; | |
print_r($myRespArr['access_token']); | |
print "</pre>"; | |
exit; | |
} | |
?> | |
<html> | |
<head> | |
<title>Linkedin Api </title> | |
</head> | |
<body> | |
<a href="<?php echo $url?>">Click Link | |
</a> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment