How to use LinkedIn service API - PHP & WRT widget example
Article Metadata
Contents |
Introduction
In this Wiki article we are checking how to get started with using the LinkedIn service API. You could implement this via e.g. your WRT widget or a web site.
The LinkedIn is a popular social connections service, and has recently opened an JavaScript API as Beta.
The full JS framework offered is imported in a size of approximately 25KB, and contains all methods to access. Our new JavaScript API is now available to all developers! With just a few lines of JS, you can import a compact framework (~25KB) that provides a RESTful based APIs access and an additional "Login" and "Share" graphical functional buttons.
The Oauth 2 has been taken care by the service, so there is no requirement for manual request-signing or re-creating the backend.
Note: the API is fairly new at the moment of writing, and so the service provider is in phase of collecting feedback, and claims to be adding more features and enhancing the functionality as well. As the API is in it's early Beta, of course there still maybe some problems, but the service suggests following their developer blog for updates.
Link to the service API home page is here: https://developer.linkedin.com/apis
Prerequisites
- Code editor
- Local or remote web hosting service having PHP enabled
Note: Internet connection fees may apply depending on your agreement with your operator on data transfer. This is always the case when access to a remote web site like in this example, is required.
Example code
For this example you need to create an application on the LinkedIn site to get the required API key and Secret key. This can be done here: https://www.linkedin.com/secure/developer (Add new application).
Please check the details from the service page, as this information may be changing.
PHP part
On the server side, you need the following files for this example:
Linkedin libraries can be downloaded from here: https://developer.linkedin.com/documents/linkedins-oauth-details
auth.php // Example code
demo.php // Example code
OAuth.php // LinkedIn service libraries
linkedin.php // LinkedIn service libraries
auth.php
<?php
session_start();
$config['base_url'] = 'http://mysite/testing/linkedin/auth.php';
$config['callback_url'] = 'http://mysite/testing/linkedin/demo.php';
$config['linkedin_access'] = 'API KEY';
$config['linkedin_secret'] = 'SEC KEY';
include_once "linkedin.php";
# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
//$linkedin->debug = true;
# Now we retrieve a request token. It will be set as $linkedin->request_token
$linkedin->getRequestToken();
$_SESSION['requestToken'] = serialize($linkedin->request_token);
# With a request token in hand, we can generate an authorization URL, which we'll direct the user to
//echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "\n\n";
header("Location: " . $linkedin->generateAuthorizeUrl());
?>
demo.php
Return to this script happens for an successful OAuth process. It is set as the callback OAuth URL in your LinkedIn Application configuration.
<?php
session_start();
$config['base_url'] = 'http://mysite/testing/linkedin/auth.php';
$config['callback_url'] = 'http://mysite/testing/linkedin/demo.php';
$config['linkedin_access'] = 'API KEY';
$config['linkedin_secret'] = 'SEC KEY';
include_once "linkedin.php";
# Init with consumer information
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
//$linkedin->debug = true;
if (isset($_REQUEST['oauth_verifier'])){
$_SESSION['oauth_verifier'] = $_REQUEST['oauth_verifier'];
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
header("Location: " . $config['callback_url']);
exit;
}
else{
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->access_token = unserialize($_SESSION['oauth_access_token']);
}
# Result is a $linkedin->access_token, which can make calls.
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,headline,picture-url)");
echo '<pre>';
echo 'My Profile Info';
echo $xml_response;
echo '<br />';
echo '</pre>';
$search_response = $linkedin->search("?company-name=facebook&count=10");
//echo $search_response;
$xml = simplexml_load_string($search_response);
echo '<pre>';
echo 'Look people who worked in facebook';
print_r($xml);
echo '</pre>';
?>
To see the authorization process in action, open your site url e.g. hxxp://your_site_url/testing/linkedin//auth.php
If everything went right, you should be logged in and redirected to your site url e.g. hxxp://your_site_url/testing/linkedin/demo.php
As the result, information about your LinkedIn profile should become visible in addition to some of those from FaceBook. For other searches, use this:
$search_response = $linkedin->search("?company=your_desire_company&count=10");
Full list of parameters is here:
[link]check http://developer.linkedin.com/docs/DOC-1005[/link]

