Back

Step by Step Guide to Building a LinkedIn API Integration in PHP

Aug 1, 2024 • 5 minute read

Hey there, fellow developer! Ready to dive into the world of LinkedIn API integration? Let's get cracking with this guide using the awesome zoonman/linkedin-api-php-client package. We'll keep things concise and to the point, just the way we like it.

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running
  • Composer installed (trust me, it's a lifesaver)
  • A LinkedIn Developer account (if you don't have one, go grab it now!)

Setting up Your LinkedIn Application

First things first, let's get your LinkedIn app set up:

  1. Head over to the LinkedIn Developer portal and create a new app.
  2. Snag your Client ID and Client Secret - you'll need these later.
  3. Configure your OAuth 2.0 settings. Don't forget to set your redirect URI!

Installing the LinkedIn API Client

Time to let Composer work its magic:

composer require zoonman/linkedin-api-php-client

Easy peasy, right?

Authenticating with LinkedIn

Now for the fun part - OAuth 2.0 flow:

use LinkedIn\Client; $client = new Client( 'your_client_id', 'your_client_secret' ); $loginUrl = $client->getLoginUrl(); // Redirect user to LinkedIn for authentication header('Location: ' . $loginUrl); exit;

Once the user comes back, grab that access token:

$accessToken = $client->getAccessToken($_GET['code']);

Making API Requests

With your access token in hand, the LinkedIn world is your oyster:

// Get profile info $profile = $client->get('me'); // Post an update $share = $client->post( 'ugcPosts', [ 'author' => 'urn:li:person:' . $profile['id'], 'lifecycleState' => 'PUBLISHED', 'specificContent' => [ 'com.linkedin.ugc.ShareContent' => [ 'shareCommentary' => [ 'text' => 'Check out this cool API integration!' ], 'shareMediaCategory' => 'NONE' ] ], 'visibility' => [ 'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC' ] ] );

Handling API Responses

LinkedIn sends back JSON, so let's parse it:

$profileData = json_decode($profile, true); echo "Hello, " . $profileData['localizedFirstName'];

Don't forget to catch those pesky exceptions:

try { // Your API calls here } catch (\LinkedIn\Exception $e) { echo "Oops! " . $e->getMessage(); }

Best Practices

  • Keep an eye on those rate limits. LinkedIn isn't too fond of spammers.
  • Store your access tokens securely. No one likes a data breach!

Advanced Usage

Want to level up? Try refreshing access tokens:

$newAccessToken = $client->refreshAccessToken($refreshToken);

Wrapping Up

And there you have it! You're now armed and ready to integrate LinkedIn into your PHP application. Remember, the LinkedIn API is your playground - experiment, create, and most importantly, have fun!

Need more info? Check out the official LinkedIn API docs and the zoonman/linkedin-api-php-client GitHub repo.

Now go forth and code something awesome! 🚀