LinkedIn is the world’s largest professional network with over 740 million members worldwide. As a professional social media platform, LinkedIn contains a wealth of information that can be useful for businesses and individuals looking to connect with professionals, conduct market research, recruit employees, and more.
While LinkedIn provides APIs that allow programmatic access to their platform, the process can be complex for those without a technical background. This article will provide a step-by-step guide on how to get data from LinkedIn using PHP.
Why Get LinkedIn Data with PHP?
Here are some of the key reasons you may want to use PHP to get LinkedIn data:
- PHP is a common server-side programming language – If you already have PHP experience, it makes sense to leverage it.
- Full access to LinkedIn data – The LinkedIn API provides access to detailed member profiles, company pages, group data, and more.
- Flexibility to build custom apps and analysis – You can build scripts to extract just the data you need.
- Cost effective – Accessing the API directly from PHP is free compared to using paid tools.
In summary, getting LinkedIn data via PHP gives you maximum flexibility and control over the data extraction process.
Step 1 – Get a LinkedIn Developer Account and API Keys
To access any LinkedIn APIs, you’ll need to sign up for a LinkedIn Developer Account. This is free and easy to set up:
- Go to https://www.linkedin.com/developers/ and click ‘Get Started’.
- Follow the steps to create your LinkedIn Developer Account.
- Once your account is activated, click on ‘My Apps’ in the top menu bar and register a new app.
- Fill in the required details for your app – you can select any app name and description.
- Agree to the API Terms of Use and click ‘Submit’.
This will generate an API Key and Secret Key for your app. Make sure to record these somewhere safe as you’ll need them for API requests. You’re now ready to start accessing LinkedIn data programmatically!
Step 2 – Install LinkedIn PHP Library
To simplify the process of connecting to LinkedIn from PHP, we’ll leverage a purpose built library called LinkedIn-API-client-Library. Here’s how to install it:
- Download the library code from: https://github.com/fbslee/LinkedIn-API-client-library
- Upload the ‘LinkedIn’ folder to your PHP application directory.
- Include autoload.php file in your PHP script:
require_once '/path/to/LinkedIn/autoload.php';
The library will now be ready to use. It takes care of OAuth access token handling, HTTP requests, and response parsing for us.
Step 3 – Connect to the LinkedIn API
With the library installed, we can now initialize a connection to LinkedIn from a PHP script.
First, we need to create a LinkedIn application instance, passing our API key and secret:
$app = new LinkedIn\Application('YOUR_API_KEY', 'YOUR_SECRET_KEY');
Next, we’ll fetch an OAuth access token. This grants temporary access to call API endpoints on behalf of the app:
$token = $app->getAccessToken();
Finally, we instantiate the LinkedIn client, providing the access token:
$linkedin = new LinkedIn\Client($token);
The $linkedin client will now enable us to make requests to pull LinkedIn data.
Step 4 – Construct API Calls
With our LinkedIn connection established, we can now start requesting data. LinkedIn structures its APIs as REST endpoints that return JSON data.
Here is an example call to fetch profile data for the user associated with the access token:
$response = $linkedin->api('/v2/me');
The /v2/me endpoint returns the authenticated user’s public profile information. Other endpoints allow retrieving data like:
- Company pages – /v2/companies/{id}
- Member profiles – /v2/members/{id}
- Job listings – /v2/jobs
- Group posts – /v2/groups/{id}/posts
Each endpoint has different options for filtering, selecting fields, pagination, etc. Refer to the LinkedIn API documentation for details.
Step 5 – Process the Response
The API calls will return LinkedIn data in JSON format. We can manipulate the JSON in PHP to extract the fields we need.
For example, to print out the first and last name of a user from the profile API call:
$profile = $linkedin->api('/v2/me'); $firstName = $profile['firstName']; $lastName = $profile['lastName']; echo "Name: $firstName $lastName";
We can iterate through more complex JSON structures to build datasets:
// Get company posts $posts = $linkedin->api('/v2/companies/1337/posts'); // Extract post details foreach ($posts['elements'] as $post) { echo "Title: " . $post['title']; echo "Summary: " . $post['summary']; // ... }
The data can also be encoded to formats like XML or CSV if needed:
$xml = XmlEncode($apiResponse); $csv = CsvEncode($apiResponse);
Step 6 – Handle Errors
When working with any external API, error handling is important. The LinkedIn PHP library makes it easy to check for errors:
$profile = $linkedin->api('/v2/me'); if($linkedin->isError($profile)) { echo "API error: " . $profile['message']; } else { // process data }
We can also catch specific error conditions like token expiration:
if($token->isExpired()) { // refresh access token }
Proper error handling ensures our scripts fail gracefully if the API runs into problems.
Conclusion
Accessing LinkedIn data through PHP provides flexibility to build customized scripts to extract and process professional information. The key steps are:
- Get LinkedIn API access keys
- Install the LinkedIn PHP library
- Initialize the LinkedIn client
- Construct API calls using endpoints
- Process JSON responses from the API
- Add error handling to catch API issues
Following these steps, you can create PHP scripts to pull any LinkedIn data ranging from member profiles to company pages and job listings. The information can then be integrated into business applications, analytics tools, and more.
Here is an example script putting all the pieces together to extract profile data:
<?php require_once '/LinkedIn-API-client-library/autoload.php'; $app = new LinkedIn\Application('API_KEY', 'SECRET_KEY'); $token = $app->getAccessToken(); $linkedin = new LinkedIn\Client($token); $profile = $linkedin->api('/v2/me'); if(!$linkedin->isError($profile)) { $id = $profile['id']; $firstName = $profile['firstName']; $lastName = $profile['lastName']; echo "ID: $id \n"; echo "Name: $firstName $lastName\n"; } else { echo "Error fetching profile data\n"; } ?>
This outputs the authenticated member’s LinkedIn ID and name. You can expand on this foundation to build robust LinkedIn-powered applications using PHP.