|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * linkedin-client |
| 4 | + * index.php |
| 5 | + * |
| 6 | + * PHP Version 5 |
| 7 | + * |
| 8 | + * @category Production |
| 9 | + * @package Default |
| 10 | + * @author Philipp Tkachev <[email protected]> |
| 11 | + * @date 8/17/17 22:47 |
| 12 | + * @license http://zoonman.com/license.txt linkedin-client License |
| 13 | + * @version GIT: 1.0 |
| 14 | + * @link http://zoonman.com/projects/linkedin-client |
| 15 | + */ |
| 16 | + |
| 17 | +// add Composer autoloader |
| 18 | +include_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php'; |
| 19 | + |
| 20 | +// import client class |
| 21 | +use LinkedIn\Client; |
| 22 | + |
| 23 | +// import environment variables from the environment file |
| 24 | +$dotenv = new Dotenv\Dotenv(dirname(__DIR__)); |
| 25 | +$dotenv->load(); |
| 26 | + |
| 27 | +// we need a session to keep intermediate results |
| 28 | +// you can use your own session persistence management |
| 29 | +// client doesn't depend on it |
| 30 | +session_start(); |
| 31 | + |
| 32 | +// instantiate the Linkedin client |
| 33 | +$client = new Client( |
| 34 | + getenv('LINKEDIN_CLIENT_ID'), |
| 35 | + getenv('LINKEDIN_CLIENT_SECRET') |
| 36 | +); |
| 37 | + |
| 38 | + |
| 39 | +if (isset($_GET['code'])) { // we are returning back from LinkedIn with the code |
| 40 | + if (isset($_GET['state']) && // and state parameter in place |
| 41 | + isset($_SESSION['state']) && // and we have have stored state |
| 42 | + $_GET['state'] === $_SESSION['state'] // and it is our request |
| 43 | + ) { |
| 44 | + try { |
| 45 | + // you have to set initially used redirect url to be able |
| 46 | + // to retrieve access token |
| 47 | + $client->setRedirectUrl($_SESSION['redirect_url']); |
| 48 | + // retrieve access token using code provided by LinkedIn |
| 49 | + $accessToken = $client->getAccessToken($_GET['code']); |
| 50 | + echo 'Access token:'; |
| 51 | + pp($accessToken); // print the access token content |
| 52 | + echo 'Profile:'; |
| 53 | + // perform api call to get profile information |
| 54 | + $profile = $client->api( |
| 55 | + 'people/~:(id,email-address,first-name,last-name)' |
| 56 | + ); |
| 57 | + pp($profile); // print profile information |
| 58 | + } catch (\LinkedIn\Exception $exception) { |
| 59 | + // in case of failure, provide with details |
| 60 | + pp($exception); |
| 61 | + pp($_SESSION); |
| 62 | + } |
| 63 | + echo '<a href="/">Start over</a>'; |
| 64 | + } else { |
| 65 | + // normally this shouldn't happen unless someone sits in the middle |
| 66 | + // and trying to override your state |
| 67 | + // or you are trying to change saved state during linking |
| 68 | + echo 'Invalid state!'; |
| 69 | + pp($_GET); |
| 70 | + pp($_SESSION); |
| 71 | + echo '<a href="/">Start over</a>'; |
| 72 | + } |
| 73 | + |
| 74 | +} elseif (isset($_GET['error'])) { |
| 75 | + // if you cancel during linking |
| 76 | + // you will be redirected back with reason |
| 77 | + pp($_GET); |
| 78 | + echo '<a href="/">Start over</a>'; |
| 79 | +} else { |
| 80 | + // define desired list of scopes |
| 81 | + $scopes = [ |
| 82 | + 'r_basicprofile', |
| 83 | + 'r_emailaddress', |
| 84 | + 'rw_company_admin', |
| 85 | + 'w_share', |
| 86 | + ]; |
| 87 | + $loginUrl = $client->getLoginUrl(); // get url on LinkedIn to start linking |
| 88 | + $_SESSION['state'] = $client->getState(); // save state for future validation |
| 89 | + $_SESSION['redirect_url'] = $client->getRedirectUrl(); // save redirect url for future validation |
| 90 | + echo 'LoginUrl: <a href="'.$loginUrl.'">' . $loginUrl. '</a>'; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Pretty print whatever passed in |
| 95 | + * |
| 96 | + * @param mixed $anything |
| 97 | + */ |
| 98 | +function pp($anything) |
| 99 | +{ |
| 100 | + echo '<pre>' . print_r($anything, true) . '</pre>'; |
| 101 | +} |
0 commit comments